package com.szy.chaintable;
import java.util.*;
import java.util.stream.IntStream;
public class Main {
//jindong
public static void main(String[] args) {
int[] nums = {10,2};
String s = "";
System.out.println(Arrays.toString(nums));
for (int i = 0; i < nums.length; i++) {
s += String.valueOf(nums[i]);
}
char[] c = null;
c = s.toCharArray();
System.out.println(c[1]);
}
public void zuichanghuiwenchuan(){
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
int[] arr = new int[a];
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}
Map<Integer,Integer> map = new HashMap<>();
for (int i = 0; i < arr.length; i++) {
map.put(arr[i],map.getOrDefault(arr[i],0)+1);
}
int res = 0;
for (Integer i: map.keySet()) {
System.out.println(map.get(i));
if(map.get(i) >= b) {
res ++;
}
}
System.out.println(res);
}
public void jingdogn(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
int n = s.length();
int[][] dp2 = new int[n][n];
int ans = 0;
for (int i = 0; i < n; i++) {
dp2[i][i] = 1; // 单个字符一定是好串
for (int j = i - 1; j >= 0; j--) {
char c = s.charAt(i);
char prev = s.charAt(j);
if (c - prev == 1 || c - prev == -1) {// 如果当前字符和前一个字符在字母表中相邻,则可以将当前字符添加到前面的好串中
dp2[i][j] = dp2[i - 1][j] + 1;
} else {// 否则当前字符不能添加到前面的好串中,只能单独成为一个好串
dp2[i][j] = 1;
}
ans = Math.max(ans, dp2[i][j]);
}
}
System.out.println(ans);
}
//使用小根堆
public int[] topKFrequent(int[] nums, int k) {
//先使用map来存储这个频率和元素
Map<Integer,Integer> map = new HashMap<>();
for (int i : nums){
map.put(i,map.getOrDefault(i,0)+1);
}
//从小到大 如果是 n2 - n1 就是从大到小
PriorityQueue<int[]> priorityQueue = new PriorityQueue<>((n1,n2) -> n1[1] - n2[1]);
for (Map.Entry<Integer,Integer> entry: map.entrySet()) {
if(priorityQueue.size() < k){
priorityQueue.add(new int[]{entry.getKey(),entry.getValue()});
} else{
if(entry.getValue() > priorityQueue.peek()[1]){
priorityQueue.poll();
priorityQueue.add(new int[]{entry.getKey(),entry.getValue()});
}
}
}
int[] res = new int[nums.length];
for (int i = k - 1; i >= 0; i--) {
res[i] = priorityQueue.poll()[1];
}
return res;
}
//贪心做法+小根堆
public int eatenApples(int[] apples, int[] days) {
PriorityQueue<int[]> priorityQueue = new PriorityQueue<>((n1,n2)->n1[0] - n2[0]);
//time 记录是哪一天 res记录 吃掉了几个苹果
int n = apples.length, res = 0, time = 0;
//如果天数是合法的 和 小顶堆里面还有元素
while (time < n || !priorityQueue.isEmpty()){
// 把今天的苹果加进去
if(time < n && apples[time] > 0) priorityQueue.add(new int[]{time + days[time] - 1, apples[time]});
// 如果苹果过期了 那就去掉
while(!priorityQueue.isEmpty() && priorityQueue.peek()[0] < time) priorityQueue.poll();
//如果还有苹果
if(!priorityQueue.isEmpty()) {
int[] t = priorityQueue.poll();
//如果还有苹果 而且苹果吃完一个还有 和 时间还没有过期 就再加进去
if(--t[1] > 0 && t[0] > time ) priorityQueue.add(t);
// 吃一个苹果
res ++;
}
//天数加一
time ++;
}
return res;
}
public int jump(int[] nums) {
int boder = 0;
int maxd = 0;
int step = 0;
for (int i = 0; i < nums.length - 1; i++) {
maxd = Math.max(maxd, i + nums[i]);
if(i == boder){
step ++;
boder = maxd;
}
}
return step;
}
//贪心 饼干
// 总是让第一个孩子吃饱
public int findContentChildren(int[] g, int[] s) {
int child = 0;
int cookie = 0;
while(child < g.length && cookie < s.length) {
if(g[child] <= s[cookie]) {
child ++;
}
if(child < g.length) {
cookie ++;
}
}
return child;
}
//贪心回文串
public int longestPalindrome(String s) {
// 思路就是使用HashMap 存储这个回文串 字母出现的位置和 我们所使用的字母然后 字母如果是奇数的时候则把这个串放在中间然后跟新
HashMap<Character,Integer> map = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
map.put(s.charAt(i),map.getOrDefault(s.charAt(i),0)+1);
}
int res = 0, odd = 0;
for (Character c: map.keySet()) {
int count = map.get(c);
int rem = count % 2;
res += count - rem;
if(rem == 1) {
odd = 1;
}
}
return res + odd;
}
public boolean canJump(int[] nums) {
if (nums.length == 1 ) return true;
if (nums[0] == 0) return false;
//dp[i] 表示 第i个位置能到的最远的位置 能到的最远的位置如果是等于i的话就说明就是 不对 能到的最远的位置如果是i的话就说明是到不了 否则 能到
int[] dp = new int[nums.length];
dp[0] = nums[0];
for (int i = 1; i < nums.length - 1; i++) {
dp[i] = Math.max(dp[i - 1], i + nums[i]);
if (dp[i] == i) {
return false;
}
}
return true;
}
public int maxProfit2(int[] prices) {
//分为 第i天持有股票 和 第i天不持有 股票 0 1 两种状态
// dp[i][0] dp[i][1] 最后一天没股票就是解
int[][] dp = new int[prices.length][2];
dp[0][0] = 0;
dp[0][1] = prices[0];
for (int i = 1; i < prices.length; i++) {
dp[i][0] = Math.max(dp[i-1][0], dp[i - 1][0] + prices[i]);
dp[i][1] = Math.max(dp[i -1][1], dp[i - 1][0] - prices[i]);
}
return dp[prices.length - 1][0];
}
public int uniquePaths(int m, int n) {
// dp[i][j] 总共就 i - 1 ; 或者j - 1 种走法
//极端情况 就是 在边界的时候 在最下面de时候 j 不能往下走 最 右边的时候 i不能减
int[][] dp = new int[m][n];
for (int i = 0; i < n; i++) {
dp[0][i] = 1;
}
for (int i = 0; i < m; i++) {
dp[i][0] = 1;
}
for (int i = 1; i < m; i++) {
//m是列 n是行
for (int j = 1 ; j < n; j++) {
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}
}
return dp[m-1][n-1];
}
//对dp
public int maxSubArray(int[] nums) {
if(nums.length == 1) return nums[0];
//定义dp[i] 的定义就是 以i结尾的最大子序列的值
int[] dp = new int[nums.length];
dp[0] = nums[0];
for (int i = 1; i < nums.length; i++) {
if(dp[i - 1] > 0) {
dp[i] = dp[i - 1] + nums[i];
} else{
dp[i] += nums[i];
}
}
int res = Integer.MIN_VALUE;
for (int i = 0; i < dp.length; i++) {
res = Math.max(res,dp[i]);
}
ArrayList<Integer> arrayList = new ArrayList<>();
Object[] de = arrayList.toArray();
int[] dd = arrayList.stream().mapToInt(Integer::valueOf).toArray();
return res;
}
public void longestPalindrome1(String s) {
//
}
public int countSubstrings(String s) {
if(s.length() == 1) return 1;
/*
* 总共有三种情况 第一种情况
* 一个字符串相等 隔一个字符串相等
*中间是回文 只用判断边上的是不是回文串
* */
boolean[][] dp = new boolean[s.length()][s.length()];
for (int j = 0; j < s.length(); j++) {
for (int i = 0; i <= j; i++) {
if(s.charAt(i) == s.charAt(j)){
if(j - i < 3) {//说明这一定是一个回文串
dp[i][j] = true;
} else{
dp[i][j] = dp[i + 1][j - 1];//看前一个状态是不是回文串 前一个状态是回文串的话 那么这个也是回文串
}
} else{
dp[i][j] = false;
}
}
}
int res = 0;
for (int i = 0; i < s.length(); i++) {
for (int j = 0; j < s.length(); j++) {
if(dp[i][j]) res ++;
}
}
return res;
}
public int candy(int[] ratings) {
//贪心
int[] candyNum = new int[ratings.length];
candyNum[0] = 1;//初始为1
for (int i = 1; i < ratings.length ; i++) {
if(ratings[i] > ratings[i - 1]) candyNum[i] = candyNum[i - 1] +1;
else candyNum[i] = 1;// 如果右边的孩子比左别的孩子糖果少 给右边的孩子初始化一次 是1
}
for (int i = ratings.length - 2; i >= 0; i--) {
//从前往后枚举 左别的孩子比右边的孩子还要大的话就取最大
if(ratings[i] > ratings[i + 1]) candyNum[i] = Math.max(candyNum[i],candyNum[i + 1] + 1);
}
int res = 0;
for (int i = 0; i < candyNum.length; i++) {
res += candyNum[i];
}
return res;
}
//加油站
public int canCompleteCircuit(int[] gas, int[] cost) {
int totalSum = 0;
int curSum = 0;
int index = 0;
for (int i = 0; i < gas.length; i++) {
totalSum = gas[i] - cost[i];
curSum = gas[i] - cost[i];
if(curSum < 0) {
index = (i+1) % gas.length;
curSum = 0;
}
}
if(totalSum < 0) return -1 ;
return index;
}
//最大数
public String largestNumber(int[] nums) {
String s = "";
for (int i = 0; i < nums.length; i++) {
s += String.valueOf(nums[i]);
}
char[] c = null;
c = s.toCharArray();
Arrays.sort(c);
String res = "";
for (int i = c.length - 1 ; i >= 0; i--) {
res += String.valueOf(c[i]);
}
return res;
}
// 摆动序列
public int wiggleMaxLength(int[] nums) {
if (nums.length <= 1) return nums.length;
int cur = 0;
int pre = 0;
int res = 1;
for (int i = 0; i < nums.length - 1; i++) {
cur = nums[i + 1] - nums[i];
if((cur > 0&& pre <= 0) || (cur < 0&& pre >= 0)) {
res ++;
pre = cur;//在单调的时候 判断平坡 1 222 3 4 5 2如果在外面更新这个平坡 的时候就会没办法过滤这个2
}
}
return res;
}
public int maxSubArray1(int[] nums) {
int res = Integer.MIN_VALUE;
int count = 0;
for (int i = 0; i < nums.length; i++) {
count += nums[i];
if(count > res) {
res = count;
}
if(count <= 0) count = 0;
}
return res;
}
public int maxProfit1(int[] prices) {
int res = 0;
for (int i = 1; i < prices.length; i++) {
if(prices[i] - prices[i - 1] > 0) {
res += prices[i] - prices[i - 1];
}
}
return res;
}
public boolean canJump1(int[] nums) {
if (nums.length == 1 ) return true;
int boder = 0;
for (int i = 0; i <= boder; i++) {//在更新范围内
boder = Math.max(boder,i + nums[i]);
if(boder >= nums.length - 1) return true;
}
return false;
}
public int jump1(int[] nums) {
int boder = 0;
int res = 0;
int pre = 0;
for (int i = 0; i <= nums.length; i++) {//在更新范围内
boder = Math.max(boder,i + nums[i]);
if( i == pre) {
pre = boder;
res ++;
}
}
return res;
}
public int largestSumAfterKNegations(int[] nums, int k) {
nums = IntStream.of(nums).boxed().sorted(((o1, o2) -> Math.abs(o2) - Math.abs(o1))).mapToInt(Integer::valueOf).toArray();
for (int i = 0; i < nums.length; i++) {
if(nums[i] < 0 && k > 0) {
nums[i] *= -1;
k--;
}
}
if(k % 2 == 1) nums[nums.length - 1] *= -1;
int res = 0;
for (int i = 0; i < nums.length; i++) {
res += nums[i];
}
return res;
}
//找零 遇到5的话就把5收入囊中 遇到 10 消耗一个5 然后把10 收入囊中 遇到20 消耗一个10 消耗一个5 或者消耗3张5
// public boolean lemonadeChange(int[] bills) {
//
// }
public int[][] reconstructQueue(int[][] people) {
Arrays.sort(people,(a,b) -> {
if(a[0] == b[0]) return a[1] - b[1];
return a[0] - b[0];
});
LinkedList<int[]> arrayList = new LinkedList<>();
for (int[] i: people) {
arrayList.add(i[1],i);
}
return arrayList.toArray(new int[people.length][]);
}
//背包思路 由于定义的dp为boolean类型的所以 状态转移方程就是 dp[i-1][j] | dp[i - 1][j - nums[i]];
public boolean canPartition(int[] nums) {
if(nums.length ==1) return false;
int sum = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
}
boolean[][] dp = new boolean[nums.length][sum/2 + 1];
for (int i = 0; i < nums.length; i++) {
dp[i][0] = true;
}
if(sum %2 == 1) return false;
for (int i = 1; i < nums.length; i++) {
for (int j = 1; j < dp[i].length; j++) {
dp[i][j] = dp[i - 1][j];
if(j >= nums[i]){
dp[i][j] = dp[i-1][j] | dp[i - 1][j - nums[i]];
}
}
}
return dp[nums.length - 1][sum/2];
}
public int findMaxForm(String[] strs, int m, int n) {
int length = strs.length;
int[][] cnt = new int[length][2];
for (int i = 0; i < length; i++) {
String str = strs[i];
for (char a: str.toCharArray()) {
if(a == '1'){
cnt[i][1] ++;
} else{
cnt[i][0] ++;
}
}
}
int[][][] dp = new int[length][m+1][n+1];
//初始化
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
dp[0][i][j] = (i >= cnt[0][0] && j >= cnt[0][1]) ? 1 : 0;
}
}
for (int k = 1; k < length; k++) {
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
dp[k][i][j] = dp[k-1][i][j];
if(i >= cnt[k][0] && j >= cnt[k][1])
dp[k][i][j] = Math.max(dp[k-1][i][j],dp[k-1][i - cnt[k][0]][j - cnt[k][1]] + 1);
}
}
}
return dp[length - 1][m][n];
}
public static int findTargetSumWays(int[] nums, int s) {
int sum = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
}
int length = nums.length;
int t = sum*2 + 1; //有可能是负
int[][] dp = new int[length][t];
if(nums[0] == 0) {
dp[0][nums[0]] =2;
} else{
dp[0][nums[0]] =1;
}
// 对dp[i][0]的初始化,可以放在下面整个dp的递推代码中
for(int i = 1; i < length; i++){
if(nums[i] == 0){
// 当nums[i]为0时,选或者不选nums[i]都可以使总和为0,即dp[i][0] = dp[i - 1][0] + dp[i - 1][0 - nums[i]] = 2 * dp[i -1][0]
dp[i][0] = 2 * dp[i -1][0];
}else{
// 当nums[i]不为0时,只有不选nums[i]才可以使总和为0
dp[i][0] = dp[i - 1][0];
}
}
for (int i = 1; i < length; i++) {
for (int j = 0; j <= sum; j++) {
if(j >= nums[i])
dp[i][j] = dp[i-1][j-nums[i]] + dp[i-1][j + nums[i]];
else{
dp[i][j] = dp[i - 1][j];
}
}
}
return dp[length - 1][s];
}
public boolean wordBreak(String s, List<String> wordDict) {
//动态规划
return true;
}
// public List<List<Integer>> combine(int n, int k) {//回溯
// combineHelper(n, k, 1);
// return res;
// }
//
// private void combineHelper(int n, int k, int start) {
// if(ans.size() == k) {
// res.add(new ArrayList<>(ans));
// return;
// }
//
// for(int i = start; i <= n - (k - ans.size()) + 1; i ++) {
// ans.add(i);
// combineHelper(n,k, i + 1);
// ans.removeLast();
// }
// }
List<List<Integer>> res = new ArrayList<>();
LinkedList ans = new LinkedList<>();
int sum = 0;
public List<List<Integer>> combinationSum3(int k, int n) {
combine3Helper(n, k, 1);
return res;
}
private void combine3Helper(int n, int k, int start) {
if (ans.size() == k ) {
if(sum == n)
res.add(new ArrayList<>(ans));
return;
}
for (int i = start; i <= 9 - (k - ans.size()) + 1; i++) {
sum += i;
String[] numString = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
ans.add(i);
combine3Helper(n,k,i+1);
sum -= i;
ans.removeLast();
}
}
List<String> list = new ArrayList<>();
public List<String> letterCombinations(String digits) {
if (digits == null || digits.length() == 0) {
return list;
}
String[] numString = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
backTracking(digits, numString, 0);
return list;
}
StringBuilder temp = new StringBuilder();
private void backTracking(String digits, String[] numString, int num) {
if(num == digits.length()){
list.add(temp.toString());
return ;
}
String str = numString[digits.charAt(num) - '0'];
for (int i = 0; i < str.length(); i++) {
temp.append(str.charAt(i));
backTracking(digits, numString, num + 1);
temp.deleteCharAt(temp.length() - 1);
}
}
// public int maxProfit(int[] prices) {
// int[][] dp = new int[prices.length][2];
// //持有股票 和 不持有股票 昨天买的 和 以前就有 以前就不持有
// dp[0][0] = 0;
// dp[0][1] = -prices[0];
// for (int i = 1; i < prices.length; i++) {
// dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1]+prices[i]);
// dp[i][1] = Math.max(dp[i - 1][1], -prices[i]);
//
// }
//
// return dp[prices.length - 1][0];
// }
public void nextPermutation(int[] nums) {
for (int i = nums.length - 2; i >= 0 ; i--) {
for (int j = nums.length - 1; j >= 0; j--) {
if(nums[i] < nums[j]) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
Arrays.sort(nums,i+1,nums.length);
}
if(i == 0) {
Arrays.sort(nums);
}
}
}
}
//状态dp
public int maxProfit(int[] prices) {
int[][] dp = new int[prices.length][5];
// dp[i][1] 表示 第一天买入 第一天卖出 第二次买入 第二次卖出
dp[0][0] = 0;
dp[0][1] = -prices[0];
dp[0][3] = -prices[0];
for (int i = 1; i < prices.length; i++) {
dp[i][0] = dp[i - 1][0];
dp[i][1] = Math.max(dp[i - 1][1],dp[i - 1][0] - prices[i]);
dp[i][2] = Math.max(dp[i - 1][2],dp[i - 1][1] + prices[i]);
dp[i][3] = Math.max(dp[i - 1][3],dp[i - 1][2] - prices[i]);
dp[i][4] = Math.max(dp[i - 1][4],dp[i - 1][3] + prices[i]);
}
return dp[prices.length - 1][2] + dp[prices.length - 1][4];
}
// public int maxProfit(int[] prices, int fee) {
//
// int[][] dp = new int[prices.length][2];
//
// }
//定义 并查集 :
int father[];
void init(int n) {
father = new int[n];
for (int i = 0; i < n; i++) {
father[i] = i;
}
}
//寻找操作
int find(int u) {
if(u == father[u]) {
return u;
} else{
return father[u] = find(father[u]);
}
}
//合并操作
void union(int u , int v) {
u = find(u);
v = find(v);
if(u == v) return;
father[v] = u;
}
//判断
boolean bool(int u, int v) {
u = find(u);
v = find(v);
return u == v;
}
public boolean validPath(int n, int[][] edges, int source, int destination) {
init(n);
for (int i = 0; i < edges.length; i++) {
union(edges[i][0],edges[i][1]);
}
return bool(source,destination);
}
public int longestConsecutive(int[] nums) {
if (nums.length == 0 || nums.length == 1) return nums[0];
Set<Integer> set = new HashSet<>();
for (int i = 0; i < nums.length; i++) {
set.add(nums[i]);
}
int res = 1;
init(set.size());
for (Integer i : set) {
if(set.contains(i - 1)) {
union(i, i - 1);
};
if(set.contains(i + 1)) {
union(i, i + 1);
}
}
for (int i = 0; i < father.length; i++) {
if(i == father[i]) {
res = Math.max(res, father[i]);
}
}
return res;
//
// for (Integer i: set) {
// if(!set.contains(i - 1)) {
// int pre = i;
// int cur = 1;
// while(set.contains(pre + 1)){
// pre += 1;
// cur += 1;
// }
// res = Math.max(res, cur);
// }
// }
// return res;
}
public int compress(char[] chars) {
int len = 0;
//len 记录数组有几个
// cnt 记录字符有几个
for (int i = 0, cnt = 1; i < chars.length; i ++, cnt ++) {
if(i + 1 == chars.length || chars[i] != chars[i + 1]) { //i+ 1 == length 说明满了 最后一个了
chars[len ++] = chars[i];
if(cnt > 1) { // 判断是不是 单个的字符
String s = "" + cnt;
for (char d : s.toCharArray()) {
chars[len ++] = d;
}
}
cnt = 0;
}
}
return len;
}
public int[] findRedundantConnection(int[][] edges) {
int[] father = new int[1001];
for (int i = 0; i < edges.length; i++) {
father[i] = i;
}
for (int i = 0; i < edges.length; i++) {
int nums1 = find(edges[i][0], father);
int nums2 = find(edges[i][1],father);
if(nums1 == nums2) {
return edges[i];
} else{
father[nums1] = nums2;
}
}
return new int[]{0,0};
}
int find(int n, int[] father) {
while(father[n] != n){
n = father[n];
}
return n;
}
public int[] distinctDifferenceArray(int[] nums) {
int[] res = new int[5];
for(int i = 0; i < 5; i ++) {
Set<Integer> set1 = new HashSet<>();
Set<Integer> set2 = new HashSet<>();
for(int j = 0; j <= i ; j ++) {
set1.add(nums[j]);
}
for (int j = i + 1; j < 5; j++) {
set2.add(nums[j]);
}
res[i] = set1.size() - set2.size();
}
return res;
}
private boolean[] jd;
List<Integer> l = new ArrayList<>();
List<List<Integer>> ll = new ArrayList<>();
public List<List<Integer>> permute(int[] nums) {
jd = new boolean[nums.length];
f(nums);
return ll;
}
private void f(int[] nums) {
if(l.size() == nums.length || !ll.contains(l)) {
ll.add(new ArrayList<>(l));
return;
}
for (int i = 0; i < nums.length; i++) {
if(jd[i]) continue;
l.add(nums[i]);
jd[i] = true;
f(nums);
l.remove(l.size() - 1);
jd[i] = false;
}
}
public int firstUniqChar(String s) {
int[] arr = new int[26];
for(int i = 0; i < s.length(); i ++) {
arr[s.charAt(i) - 'a'] ++;
}
for (int i = 0; i < s.length(); i++) {
if(arr[s.charAt(i) - 'a'] == 1) {
return i;
}
}
return -1;
}
public String replaceSpace(String s) {
for (int i = 0; i < s.length(); i++) {
s.replace(" ","%20");
}
return s;
}
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public int[] reversePrint(ListNode head) {
if(head == null) return new int[]{};
ListNode node = reverse(head);
List<Integer> arr = new ArrayList<>();
while(node != null) {
arr.add(node.val);
node = node.next;
}
return arr.stream().mapToInt(Integer::valueOf).toArray();
}
static ListNode reverse(ListNode head) {
if(head == null || head.next == null) return head;
ListNode node = reverse(head.next);
head.next.next = head;
head.next = null;
return node;
}
public boolean isNumber(String s) {
s = s.trim();
boolean numFlag = false;//数字
boolean dotFlag = false;//+-.
boolean eFlag = false;//Ee
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c >= '0' && c <= '9') {// 判断是不是 0到9
numFlag = true;
} else if (c == '.' && !dotFlag && !eFlag) { // 判断 . 的时候 要前面不能有 e 和 符号
dotFlag = true;
} else if ((s.charAt(i) == 'e' || s.charAt(i) == 'E') && !eFlag && numFlag) { //判断 是e E 的时候 前面不能有e 和必须有数字
eFlag = true;
numFlag = false;// 为了防止 是123e 这种恶心数子
} else if ((s.charAt(i) == '+' || s.charAt(i) == '-') && (i == 0 || s.charAt(i - 1) == 'e' || s.charAt(i - 1) == 'E')) {
//傻逼题
} else {
return false;
}
}
return numFlag;
}
public boolean validateStackSequences(int[] pushed, int[] popped) { // 傻逼题
if(pushed.length <= 2) return true;
int flag = 0;
Stack<Integer> st = new Stack<>();
for (int i = 0; i < pushed.length; i++) {
st.push(pushed[i]);
while(!st.isEmpty() && st.peek() == popped[flag]) {
flag ++;
st.pop();
}
}
return st.isEmpty();
}
public boolean findNumberIn2DArray(int[][] matrix, int target) {
for (int i = 0; i < matrix.length; i++) {
if(search(matrix[i], target)) {
return true;
};
}
return false;
}
private boolean search(int[] matrix, int k) {
int l = 0;
int r = matrix.length - 1;
while(l <= r) {
int m = (r - l) /2 + l;
if(matrix[m] == k) {
return true;
}
else if(matrix[m] > k) {
r = m - 1;
} else {
l = m + 1;
}
}
return false;
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public int[] levelOrder(TreeNode root) {
if(root == null) return null;
List<Integer> list = new ArrayList<>();
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()) {
TreeNode q = queue.poll();
list.add(q.val);
if(q.left!= null) queue.offer(q.left);
if(q.right!= null) queue.offer(q.right);
}
return list.stream().mapToInt(Integer::intValue).toArray();
}
public boolean isSubStructure(TreeNode A, TreeNode B) {
if(A == null || B == null) return false;
return dfs(A, B) || isSubStructure(A.left,B) || isSubStructure(A.right, B);// 直接看A B 然后是A的左子树 A的右子树
}
public boolean dfs(TreeNode A, TreeNode B) {
if(B == null) return true;//先要判断B B完了就说明这个树是匹配的
if(A == null) return false;
return A.val == B.val && dfs(A.left,B.left) && dfs(A.right, B.right);// 左子树和B的左子树相同 B的有字数和A的右子树相同
}
}
乱七八糟的一些leetcode 刷题代码
最新推荐文章于 2024-04-21 14:39:49 发布