map = new HashMap<>();
+
+ static {
+ map.put(1,"I");
+ map.put(2,"II");
+ map.put(3,"III");
+ map.put(4,"IV");
+ map.put(5,"V");
+ map.put(6,"VI");
+ map.put(7,"VII");
+ map.put(8,"VIII");
+ map.put(9,"IX");
+ map.put(10,"X");
+ map.put(20,"XX");
+ map.put(30,"XXX");
+ map.put(40,"XL");
+ map.put(50,"L");
+ map.put(60,"LX");
+ map.put(70,"LXX");
+ map.put(80,"LXXX");
+ map.put(90,"XC");
+ map.put(100,"C");
+ map.put(200,"CC");
+ map.put(300,"CCC");
+ map.put(400,"CD");
+ map.put(500,"D");
+ map.put(600,"DC");
+ map.put(700,"DCC");
+ map.put(800,"DCCC");
+ map.put(900,"CM");
+ map.put(1000,"M");
+ map.put(2000,"MM");
+ map.put(3000,"MMM");
+ }
+ public String intToRoman(int num) {
+
+
+ StringBuilder sb = new StringBuilder();
+ String x = String.valueOf(num);
+ for (int i = 0; i < x.length(); i++) {
+ char c = x.charAt(i);
+ if (c == '0') {
+ continue;
+ }
+ int v = (int) ((c - '0') * Math.pow(10, x.length() - i - 1));
+ sb.append(map.get(v));
+ }
+ return sb.toString();
+ }
+
+ }
+
+ @Test
+ public void test() {
+ Solution solution = new Solution();
+ System.out.println(solution.intToRoman(1994));
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode121.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode121.java
new file mode 100644
index 0000000..638adf6
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode121.java
@@ -0,0 +1,88 @@
+package cn.whaifree.leetCode.Dynamic;
+
+import org.junit.Test;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/4/3 14:38
+ * @注释
+ */
+public class LeetCode121 {
+
+ @Test
+ public void test()
+ {
+
+ int[] prices = {7,1,5,3,6,4};
+ System.out.println(new Solution1().maxProfit(prices));
+ }
+
+
+
+ class Solution {
+
+ /**
+ *
+ * 只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。
+ *
+ * dp[i][1]表示i天不持有股票的持有现金
+ * dp[i][0]表示i天持有股票所有的现金
+ * 第i天买入股票现金就是 -prices[i]
+ *
+ * 第i天有股票 1. 刚刚买入、2. 第i-1天就有
+ * 第i天没有股票 2. 第i天卖出 2. 第i-1天就没有股票
+ *
+ * dp[i][0] 持有股票
+ * - i-1就持有股票,今天不卖 dp[i-1][0]
+ * - 买入股票 -今天的股价 -prices[i]
+ * - max
+ * dp[i][1] 无持有
+ * - i-1 保持现状dp[i-1][1]
+ * - 卖出 prices[i] + dp[i-1][0] 价格+[i-1]天加持有的现金
+ * - max
+ * dp[0][0] = -price[0]
+ * dp[0][1] = 0
+ *
+ * @param prices
+ * @return
+ */
+ public int maxProfit(int[] prices) {
+
+ if (prices.length == 1) {
+ return 0;
+ }
+
+ int[] dp = new int[2];
+
+ dp[0] = -prices[0];
+ dp[1] = 0;
+ for (int i = 0; i < prices.length; i++) {
+ // 持有股票 1. i-1就有 2. 刚刚买入
+ dp[0] = Math.max(dp[0], -prices[i]);
+ // 无股票 2. i-1就无,2 刚刚卖出
+ dp[1] = Math.max(dp[1], dp[0] + prices[i]);
+ }
+ return dp[1]; // 最后一天一定没有股票才能有最大利润
+ }
+ }
+ class Solution1 {
+ /**
+ * 贪心
+ * @param prices
+ * @return
+ */
+ public int maxProfit(int[] prices) {
+
+ int min = Integer.MAX_VALUE;
+ int res = 0;
+ for (int i = 0; i < prices.length; i++) {
+ min = Math.min(min, prices[i]);
+ res = Math.max(res, prices[i] - min);
+ }
+ return res;
+ }
+ }
+
+
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode122.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode122.java
new file mode 100644
index 0000000..9f508ed
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode122.java
@@ -0,0 +1,70 @@
+package cn.whaifree.leetCode.Greedy;
+
+import org.junit.Test;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/2/25 22:21
+ * @注释
+ */
+public class LeetCode122 {
+
+
+ @Test
+ public void test() {
+
+ System.out.println(new Solution1().maxProfit(new int[]{7, 1, 5, 3, 6, 4}));
+
+ System.out.println(new Solution1().maxProfit(new int[]{1}));
+
+
+ }
+
+ class Solution {
+
+
+ /**
+ * 随时可以买卖,那么只要有跌的我都不要。
+ * 上帝视角:只要涨我就全要
+ *
+ * 7跌 1涨5 跌 4涨6 跌4
+ *
+ * @param prices
+ * @return
+ */
+ public int maxProfit(int[] prices) {
+
+ int maxProfit = 0;
+ for (int i = 0; i < prices.length; i++) {
+ if (i > 0 && prices[i] - prices[i - 1] > 0) {
+ maxProfit += prices[i] - prices[i - 1];
+ }
+ }
+ return maxProfit;
+ }
+
+ }
+
+ class Solution1 {
+
+ /**
+ * dp[i] 表示当天可获得的收益
+ * dp[i] = dp[i-1] + if(prices[i-1] < price[i]){prices[i] - price[i-1]}
+ *
+ * @param prices
+ * @return
+ */
+ public int maxProfit(int[] prices) {
+ int[] dp = new int[prices.length];
+ for (int i = 1; i < prices.length; i++) {
+ dp[i] = dp[i - 1];
+ if (prices[i - 1] < prices[i]) {
+ // 挣钱
+ dp[i] += prices[i] - prices[i - 1];
+ }
+ }
+ return dp[prices.length - 1];
+ }
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode124.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode124.java
new file mode 100644
index 0000000..4b9aeb5
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode124.java
@@ -0,0 +1,56 @@
+package cn.whaifree.leetCode.Tree;
+
+import cn.whaifree.leetCode.model.TreeNode;
+import org.junit.Test;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/9/16 15:05
+ * @注释
+ */
+public class LeetCode124 {
+ @Test
+ public void test()
+ {
+
+ TreeNode treeNode = TreeNode.constructTreeByArray(1, 2, 3);
+ System.out.println(new Solution().maxPathSum(treeNode));
+ }
+
+ class Solution {
+ /**
+ * f
+ * a
+ * b c
+ *
+ * a有可能的路径
+ * 1. f a c
+ * 2. f a b
+ * 3. b a c 不包含父亲节点 用b+a+c与max判断
+ *
+ * @param root
+ * @return
+ */
+ int max = Integer.MIN_VALUE;
+ public int maxPathSum(TreeNode root) {
+ maxPath(root);
+ return max;
+ }
+ public int maxPath(TreeNode root) {
+ if (root == null) {
+ return 0;
+ }
+ int left = maxPath(root.left);
+ int right = maxPath(root.right);
+ if (left < 0) {
+ left = 0;
+ }
+ if (right < 0) {
+ right = 0;
+ }
+ max = Math.max(max, left + right + root.val); // 如果是 b a c 既没有用父亲节点
+ return Math.max(left, right) + root.val;
+ }
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode125.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode125.java
new file mode 100644
index 0000000..2f561cc
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode125.java
@@ -0,0 +1,49 @@
+package cn.whaifree.leetCode.String;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/9/18 23:28
+ * @注释
+ */
+public class LeetCode125 {
+ public static void main(String[] args) {
+ System.out.println((int) 'A');
+ System.out.println((int) 'Z');
+ System.out.println((int) 'a');
+ System.out.println((int) 'z');
+ System.out.println(new Solution().isPalindrome("A man, a plan, a canal: Panama"));
+
+ }
+
+ static class Solution {
+ public boolean isPalindrome(String s) {
+ StringBuilder sb = new StringBuilder();
+ for (char c : s.toCharArray()) {
+ // 大写字符转换为小写字符、并移除所有非字母数字字符
+// if (c >= 'A' && c <= 'Z') {
+// sb.append((char) (c + 32));
+// } else if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
+// sb.append(c);
+// }
+ if (Character.isLetterOrDigit(c)) {
+ sb.append(Character.toLowerCase(c));
+ }
+
+ }
+ return isHuiWen(sb.toString());
+ }
+ public boolean isHuiWen(String s) {
+ int left = 0;
+ int right = s.length() - 1;
+ while (left < right) {
+ if (s.charAt(left) != s.charAt(right)) {
+ return false;
+ }
+ left++;
+ right--;
+ }
+ return true;
+ }
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode129.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode129.java
new file mode 100644
index 0000000..80c9da2
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode129.java
@@ -0,0 +1,38 @@
+package cn.whaifree.leetCode.LinkedList;
+
+import cn.whaifree.leetCode.model.TreeNode;
+import org.junit.Test;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/10/27 22:27
+ * @注释
+ */
+public class LeetCode129 {
+
+ @Test
+ public void test() {
+ TreeNode treeNode = TreeNode.constructTreeByArray(new Integer[]{4, 9, 0, 5, 1});
+ int sum = new Solution().sumNumbers(treeNode);
+ System.out.println(sum);
+ }
+
+ class Solution {
+ public int sumNumbers(TreeNode root) {
+ return sumNumbers(root, 0);
+ }
+ public int sumNumbers(TreeNode root,int pre) {
+ if (root == null) {
+ return 0;
+ }
+ if (root.left == null && root.right == null) {
+ return pre * 10 + root.val;
+ }
+ int now = pre * 10 + root.val;
+ int left = sumNumbers(root.left, now);
+ int right = sumNumbers(root.right, now);
+ return left + right;
+ }
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode129_2.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode129_2.java
new file mode 100644
index 0000000..8c5e678
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode129_2.java
@@ -0,0 +1,38 @@
+package cn.whaifree.leetCode.Tree;
+
+import cn.whaifree.leetCode.model.TreeNode;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/10/15 12:34
+ * @注释
+ */
+
+
+public class LeetCode129_2 {
+
+
+ /**
+ * 向下遍历,从前往后获取值
+ */
+ class Solution {
+
+ public int sumNumbers(TreeNode root) {
+ return level(root, 0);
+ }
+
+ public int level(TreeNode root,int preV) {
+ if (root == null) {
+ return 0;
+ }
+ int sum = root.val + preV * 10;
+ if (root.left == null && root.right == null) {
+ return sum;
+ }
+ int right = level(root.right,sum);
+ int left = level(root.left, sum);
+ return right + left;
+ }
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode13.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode13.java
new file mode 100644
index 0000000..62f43b6
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode13.java
@@ -0,0 +1,123 @@
+package cn.whaifree.leetCode.String;
+
+import org.junit.Test;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/9/18 0:11
+ * @注释
+ */
+public class LeetCode13 {
+
+ @Test
+ public void test()
+ {
+ Solution1 solution = new Solution1();
+ int i = solution.romanToInt("MCMXCIV");
+ System.out.println(i);
+ }
+
+ class Solution {
+ public int romanToInt(String s) {
+ char[] charArray = s.toCharArray();
+ int res = 0;
+ for (int i = 0; i < charArray.length; i++) {
+ if (i < charArray.length - 1 && charArray[i] == 'I' && charArray[i + 1] == 'V') {
+ res += 4;
+ i++;
+ }
+ else if (i < charArray.length - 1 && charArray[i] == 'I' && charArray[i + 1] == 'X') {
+ res += 9;
+ i++;
+ }
+ else if (i < charArray.length - 1 && charArray[i] == 'X' && charArray[i + 1] == 'L') {
+ res += 40;
+ i++;
+ }
+ else if (i < charArray.length - 1 && charArray[i] == 'X' && charArray[i + 1] == 'C') {
+ res += 90;
+ i++;
+ }
+ else if (i < charArray.length - 1 && charArray[i] == 'C' && charArray[i + 1] == 'D') {
+ res += 400;
+ i++;
+ }
+ else if (i < charArray.length - 1 && charArray[i] == 'C' && charArray[i + 1] == 'M') {
+ res += 900;
+ i++;
+ }
+ else if (charArray[i] == 'I') {
+ res += 1;
+ }
+ else if (charArray[i] == 'V') {
+ res += 5;
+ }
+ else if (charArray[i] == 'X') {
+ res += 10;
+ }
+ else if (charArray[i] == 'L') {
+ res += 50;
+ }
+ else if (charArray[i] == 'C') {
+ res += 100;
+ }
+ else if (charArray[i] == 'D') {
+ res += 500;
+ }
+ else if (charArray[i] == 'M') {
+ res += 1000;
+ }
+ }
+ return res;
+ }
+ }
+
+ class Solution1 {
+
+
+ /**
+ * 当前位置的元素比下个位置的元素小,就减去当前值,否则加上当前值
+ * 输入: s = "MCMXCIV"
+ * 输出: 1994
+ * 解释: M = 1000, CM = 900, XC = 90, IV = 4.
+ * @param s
+ * @return
+ */
+ public int romanToInt(String s) {
+ int res = 0;
+ int preV = getV(s.charAt(0));
+ for (int i = 1; i < s.length(); i++) {
+ int nums = getV(s.charAt(i));
+ if (preV < nums) {
+ res -= preV;
+ }else {
+ res += preV;
+ }
+ preV = nums;
+ }
+ res += preV;
+ return res;
+ }
+
+ public int getV(char c) {
+ switch (c) {
+ case 'I':
+ return 1;
+ case 'V':
+ return 5;
+ case 'X':
+ return 10;
+ case 'L':
+ return 50;
+ case 'C':
+ return 100;
+ case 'D':
+ return 500;
+ case 'M':
+ return 1000;
+ }
+ return 0;
+ }
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode131.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode131.java
new file mode 100644
index 0000000..84f0c12
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode131.java
@@ -0,0 +1,71 @@
+package cn.whaifree.leetCode.BackTracking;
+
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/2/10 15:12
+ * @注释
+ */
+public class LeetCode131 {
+
+ @Test
+ public void test() {
+ String abbc = "aaab";
+ new Solution().partition(abbc).forEach(
+ list -> {
+ System.out.println(list);
+ }
+ );
+ }
+
+ class Solution {
+
+ List> res = new ArrayList<>();
+ List path = new ArrayList<>();
+ public List> partition(String s) {
+ backTracking(s, 0);
+ return res;
+ }
+
+ /**
+ * 1. 判断回文字符串函数
+ * 2. 从start开始切割,到i i++
+ * 递归
+ *
+ *
+ * @param s
+ * @param start
+ */
+ public void backTracking(String s, int start) {
+ if (start >= s.length()) {
+ res.add(new ArrayList<>(path));
+ return;
+ }
+ for (int i = start; i < s.length(); i++) {
+ if (isPalindrome(s, start, i)) {
+ String substring = s.substring(start, i + 1);
+ path.add(substring);
+ backTracking(s, i + 1);
+ path.remove(path.size() - 1);
+ }
+ }
+
+ }
+
+ //判断是否是回文串
+ private boolean isPalindrome(String s, int startIndex, int end) {
+ for (int i = startIndex, j = end; i < j; i++, j--) {
+ if (s.charAt(i) != s.charAt(j)) {
+ return false;
+ }
+ }
+ return true;
+ }
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode134.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode134.java
new file mode 100644
index 0000000..a36b9c5
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode134.java
@@ -0,0 +1,120 @@
+package cn.whaifree.leetCode.Greedy;
+
+import org.junit.Test;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/2/27 20:02
+ * @注释
+ */
+public class LeetCode134 {
+
+ @Test
+ public void test() throws InterruptedException {
+
+ System.out.println("初始化内存 -Xms");
+ System.out.println(Runtime.getRuntime().totalMemory() / 1024 / 1024 + "m");
+
+ System.out.println("最大可使用内存 :");
+ System.out.println(Runtime.getRuntime().freeMemory() / 1024 / 1024 + "m");
+
+ System.out.println("最大堆内存:-Xmx");
+ System.out.println(Runtime.getRuntime().maxMemory() / 1024 / 1024 + "m");
+
+ // -XX+PrintGCDetails
+
+ int[] ints = new int[10000];
+// Thread.sleep(1000000);
+
+// System.out.println(new Solution().canCompleteCircuit(
+// new int[]{5,1,2,3,4},
+// new int[]{4,4,1,5,1}
+// ));
+
+ }
+
+ class Solution {
+ /**
+ *
+ * @param gas 加油站有的油
+ * @param cost 行驶代价
+ * @return
+ */
+ public int canCompleteCircuit(int[] gas, int[] cost) {
+
+ int[] rent = new int[gas.length];
+ int total = 0;
+ for (int i = 0; i < gas.length; i++) {
+ rent[i] = gas[i] - cost[i];
+ total += rent[i];
+ }
+ // 如果全部剩余<0 则必然跑不了一圈
+ if (total < 0) {
+ return -1;
+ }
+
+
+ // 以下为必然可以跑一圈的
+ // 如果当前剩余不够用,则让指针指向i+1
+ int curSum = 0;
+ int index = 0;
+ for (int i = 0; i < rent.length; i++) {
+ curSum += rent[i];
+ if (curSum < 0) {
+ index = (i + 1) % gas.length ;
+ curSum = 0;
+ }
+ }
+ // 1. 前提,必然能跑一圈
+ // 2. 没有进入某个i之后都没有curSum<0 那么就是正确的i
+ return index;
+ }
+
+ }
+// class Solution {
+// /**
+// *
+// * @param gas 加油站有的油
+// * @param cost 行驶代价
+// * @return
+// */
+// public int canCompleteCircuit(int[] gas, int[] cost) {
+// int needSum = 0;
+// for (int i : cost) {
+// needSum += i;
+// }
+//
+// int iHave = 0;
+// int sumAdd = 0;
+// int start = 0;
+// for (int i = 0; i < gas.length; i++) {
+// iHave += gas[i];
+// sumAdd += gas[i];
+// if (iHave < cost[i]) {
+// iHave = 0;
+// i = start;
+// start = start + 1;
+// sumAdd = 0;
+// continue;
+// }
+// iHave -= cost[i];
+// if (sumAdd >= needSum) {
+// return start;
+// }
+//
+// if (i == gas.length - 1) {
+// i = -1;
+// }
+//
+// if (start == gas.length - 1 && start != i) {
+// break;
+// }
+// }
+//
+// return -1;
+// }
+//
+// }
+
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode135.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode135.java
new file mode 100644
index 0000000..32fa45e
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode135.java
@@ -0,0 +1,63 @@
+package cn.whaifree.leetCode.Greedy;
+
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.Vector;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/2/28 12:44
+ * @注释
+ */
+public class LeetCode135 {
+
+ @Test
+ public void tesr() {
+ System.out.println(new Solution().candy(new int[]{1, 2, 2, 5, 4, 3, 2}));
+ }
+
+
+ class Solution {
+ public int candy(int[] ratings) {
+ // 从前往后遍历,遇到ratings[i]>ratings[i-1] score[i]+1
+ int length = ratings.length;
+
+ int[] scores = new int[length];
+ scores[0] = 1;
+
+ for (int i = 1; i < length; i++) {
+ if (ratings[i] > ratings[i - 1]) {
+ scores[i] = scores[i - 1] + 1;
+ }else {
+ scores[i] = 1;
+ }
+ }
+
+ // 从后往前
+ for (int i = length -1; i > 0; i--) {
+ /**
+ * 如果 ratings[i] < ratings[i - 1],此时candyVec[i-1](第i-1个小孩的糖果数量)就有两个选择了,
+ * - 一个是candyVec[i] + 1(从右边这个加1得到的糖果数量),
+ * - 一个是candyVec[i-1](之前比较右孩子大于左孩子得到的糖果数量)。
+ * 如 5 3
+ * 从右边向左边,i-1可选择为 3+1 或者原来的 5,从左往右已经处理过的,
+ * 同时要满足同时保证i小孩既大于左也大于右边,那么取最大值。
+ * 同时保证i小孩既大于左也大于右边
+ */
+ if (ratings[i] < ratings[i - 1]) {
+ scores[i - 1] = Math.max(scores[i] + 1, scores[i - 1]);
+ }
+ }
+
+ int scoreSum = 0;
+ for (int score : scores) {
+ scoreSum += score;
+ }
+
+ return scoreSum;
+ }
+ }
+
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode138.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode138.java
new file mode 100644
index 0000000..21dd4ce
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode138.java
@@ -0,0 +1,86 @@
+package cn.whaifree.leetCode.LinkedList;
+
+import org.junit.Test;
+
+public class LeetCode138 {
+
+ static class Node {
+ int val;
+ Node next;
+ Node random;
+
+ public Node(int val) {
+ this.val = val;
+ this.next = null;
+ this.random = null;
+ }
+ }
+
+ @Test
+ public void test() {
+
+ // 1 2 3
+ // 1 3
+ // 2 1
+
+// Node node1 = new Node(1);
+// Node node2 = new Node(2);
+// Node node3 = new Node(3);
+// node1.next = node2;
+// node2.next = node3;
+// node1.random = node3;
+// node2.random = node1;
+// Node node = copyRandomList(node1);
+// System.out.println(node);
+
+
+// [[7,null],[13,0],[11,4],[10,2],[1,0]]
+ Node node1 = new Node(7);
+ Node node2 = new Node(13);
+ Node node3 = new Node(11);
+ Node node4 = new Node(10);
+ Node node5 = new Node(1);
+ node1.next = node2;
+ node2.next = node3;
+ node3.next = node4;
+ node4.next = node5;
+ node2.random = node1;
+ node3.random = node5;
+ node4.random = node3;
+ node5.random = node1;
+ Node node = copyRandomList(node1);
+ System.out.println(node);
+ }
+
+ public Node copyRandomList(Node head) {
+ Node index = head;
+ while (index != null) {
+ Node tmp = index.next;
+ index.next = new Node(index.val);
+ index.next.next = tmp;
+ index = index.next.next;
+ }
+
+ index = head;
+ while (index != null) {
+ if (index.random != null) {
+ index.next.random = index.random.next;
+ }
+ index = index.next.next;
+ }
+
+
+ Node newHead = head.next;
+ index = newHead;
+ Node tmpPreIndex = head;
+ while (index.next != null) {
+ tmpPreIndex.next = tmpPreIndex.next.next;
+ tmpPreIndex = tmpPreIndex.next;
+ index.next = index.next.next;
+ index = index.next;
+ }
+ tmpPreIndex.next = null;
+ return newHead;
+ }
+
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode139.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode139.java
new file mode 100644
index 0000000..447d4dd
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode139.java
@@ -0,0 +1,84 @@
+package cn.whaifree.leetCode.Dynamic;
+
+import org.junit.Test;
+
+import java.util.List;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/3/28 13:15
+ * @注释
+ */
+public class LeetCode139 {
+
+ @Test
+ public void test() {
+// System.out.println(new Solution().wordBreak("leetcode", List.of("leet", "code")));
+// System.out.println(new Solution().wordBreak("leetcode", List.of("leet", "cod", "e")));
+ // ["apple","pen"]
+ System.out.println(new Solution().wordBreak("applepenapple", List.of("apple", "pen")));
+
+ }
+
+ class Solution {
+
+ /**
+ *
+ * dp[i][j] 表示s中前j个字母能否被0-i中任意元素组合出来
+ *
+ * 定义 dp[i] 表示字符串 s 前 i 个字符组成的字符串 s[0..i−1] 是否能被空格拆分成若干个字典中出现的单词
+ *
+ * c a t s a n d o g
+ * cats 0 0 0 1 0
+ * dog
+ * sand
+ * and
+ * cat
+ * '' l e e t c o d e
+ * '' 1 0 0 0 0 0 0 0 0
+ * leet 1 0 0 0 1 0 0 0 0
+ * code 1 0 0 0 0 0 0 0 1
+ *
+ * a p p l e p e n a p p l e
+ * apple 1 0 0 0 0 1 0 0 0 0 0 0 0 1
+ * pen 1 0 0 0 0 1 0 0 1 0 0 0 0 0
+ *
+ * @param s
+ * @param wordDict
+ * @return
+ */
+ public boolean wordBreak(String s, List wordDict) {
+ // 完全背包,先便利字符串再遍历背包,确保重会重复选择背包
+ int len = s.length();
+ boolean[] dp = new boolean[len + 1];
+ dp[0] = true;
+ for (int j = 1; j <= len; j++) {
+ for (int i = 0; i < wordDict.size(); i++) {
+ String str = wordDict.get(i);
+ int index = j - str.length();
+ if (index >= 0 && dp[index] && check(s, j - 1, str)) {
+ dp[j] = true;
+ }
+ }
+ }
+ return dp[len];
+ }
+
+
+
+ public boolean check(String s, int start, String word) {
+ for (int i = word.length() - 1; i >= 0; i--) {
+ if (word.charAt(i) != s.charAt(start)) {
+ return false;
+ }
+ start--;
+ }
+ return true;
+ }
+ }
+ @Test
+ public void main() {
+ System.out.println(new Solution().check("leetcode", 7, "code"));
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode14.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode14.java
new file mode 100644
index 0000000..99e50fa
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode14.java
@@ -0,0 +1,73 @@
+package cn.whaifree.leetCode.String;
+
+import org.junit.Test;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/9/18 22:09
+ * @注释
+ */
+public class LeetCode14 {
+ @Test
+ public void test() {
+ Solution solution = new Solution();
+ String[] strs = {"d", "c"};
+ String s = solution.longestCommonPrefix(strs);
+ System.out.println(s);
+ }
+
+ class Solution {
+ public String longestCommonPrefix(String[] strs) {
+
+ String min = strs[0];
+ for (String s : strs) {
+ if (s.length() < min.length()) {
+ min = s;
+ }
+ }
+
+ int index = 0;
+
+ while (index < min.length()) {
+ char c = strs[0].charAt(index);
+ for (int i = 1; i < strs.length; i++) {
+ if (strs[i].charAt(index) != c) {
+ return strs[0].substring(0, index);
+ }
+ }
+ index++;
+ }
+
+ return min;
+ }
+ }
+
+ @Test
+ public void test2() {
+ Solution2 solution = new Solution2();
+ String[] strs = {"flower","flow","flight"};
+ String s = solution.longestCommonPrefix(strs);
+ System.out.println(s);
+ }
+
+ class Solution2 {
+ public String longestCommonPrefix(String[] strs) {
+ String str = strs[0];
+ for (int i = 1; i < strs.length; i++) {
+ str = common(str, strs[i]);
+ }
+ return str;
+ }
+
+ public String common(String a, String b) {
+ int len = Math.min(a.length(), b.length());
+ for (int i = 0; i < len; i++) {
+ if (a.charAt(i) != b.charAt(i)) {
+ return a.substring(0, i);
+ }
+ }
+ return a.substring(0, len);
+ }
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode141.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode141.java
new file mode 100644
index 0000000..f97761b
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode141.java
@@ -0,0 +1,42 @@
+package cn.whaifree.leetCode.LinkedList;
+
+import cn.whaifree.leetCode.model.ListNode;
+import org.junit.Test;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/9/23 12:54
+ * @注释
+ */
+public class LeetCode141 {
+ @Test
+ public void test() {
+ ListNode listNode = ListNode.listNodeFromArray(new int[]{1});
+// listNode.next.next.next = listNode;
+ System.out.println(new Solution().hasCycle(listNode));
+ }
+
+ public class Solution {
+ public boolean hasCycle(ListNode head) {
+ if (head == null) {
+ return false;
+ }
+ if (head.next == null) {
+ return false; // 只有一个节点
+ }
+ ListNode slow = head;
+ ListNode fast = head.next;
+ while (slow != fast) {
+ if (fast == null || fast.next == null) {
+ return false;
+ }
+ slow = slow.next;
+ fast = fast.next.next;
+ }
+ return true;
+
+
+ }
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode142.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode142.java
new file mode 100644
index 0000000..404c609
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode142.java
@@ -0,0 +1,104 @@
+package cn.whaifree.leetCode.LinkedList;
+
+import cn.whaifree.leetCode.model.ListNode;
+import org.junit.Test;
+
+import java.util.ArrayDeque;
+import java.util.HashSet;
+import java.util.Objects;
+import java.util.Stack;
+
+public class LeetCode142 {
+
+ @Test
+ public void test() {
+
+
+
+ Solution solution = new Solution();
+ ListNode listNode1= new ListNode(1);
+ ListNode listNode2= new ListNode(2,listNode1);
+ ListNode listNode3= new ListNode(3,listNode2);
+ ListNode listNode4= new ListNode(4,listNode3);
+ listNode1.next = listNode3;
+
+// ListNode.printList(listNode4);
+
+ ListNode listNode = solution.detectCycle(listNode4);
+ if (listNode == null) {
+ System.out.println("null");
+ } else {
+ System.out.println(listNode.val);
+ }
+ }
+
+ /**
+ * Definition for singly-linked list.
+ * class ListNode {
+ * int val;
+ * ListNode next;
+ * ListNode(int x) {
+ * val = x;
+ * next = null;
+ * }
+ * }
+ */
+ public class Solution {
+ /**
+ * 快慢指针
+ * 时间复杂度:O(N),其中 N 为链表中节点的数目。我们恰好需要访问链表中的每一个节点。
+ * 空间复杂度:O(1)
+ *
+ * @param head
+ * @return
+ */
+ public ListNode detectCycle(ListNode head) {
+
+ ListNode slow = head;
+ ListNode fast = head;
+ while (fast != null && fast.next != null) {
+ slow = slow.next;
+ fast = fast.next.next;
+ // 相遇了,有环。
+ if (slow == fast) {
+ break;
+ }
+ }
+
+ if (Objects.isNull(fast) || Objects.isNull(fast.next)) {
+ return null;
+ }
+
+ // 此时相遇 fast,slow
+ // x = (n-1)(y-z)+z
+ slow = head;
+ while (slow != fast) {
+ slow = slow.next;
+ fast = fast.next;
+ }
+ return slow;
+ }
+ }
+
+
+ /**
+ * 时间复杂度:O(N),其中 N 为链表中节点的数目。我们恰好需要访问链表中的每一个节点。
+ * 空间复杂度:O(N),其中 N 为链表中节点的数目。我们需要将链表中的每个节点都保存在哈希表当中。
+ */
+ public class Solution1 {
+ /**
+ * 快慢指针
+ * @param head
+ * @return
+ */
+ public ListNode detectCycle(ListNode head) {
+ HashSet listNodes = new HashSet<>();
+ ListNode index = head;
+ while (index != null && !listNodes.contains(index)) {
+ listNodes.add(index);
+ index = index.next;
+ }
+ return index;
+ }
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode144.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode144.java
new file mode 100644
index 0000000..49247a3
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode144.java
@@ -0,0 +1,81 @@
+package cn.whaifree.leetCode.Tree;
+
+import cn.whaifree.leetCode.model.TreeNode;
+import org.junit.Test;
+
+import java.util.Deque;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.function.Consumer;
+
+/**
+ * 前序遍历
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/1/14 15:12
+ * @注释
+ */
+public class LeetCode144 {
+
+ @Test
+ public void test() {
+ TreeNode treeNode = TreeNode.constructTree(new Integer[]{});
+ TreeNode.printTree(treeNode);
+ new Solution1().preorderTraversal(treeNode).forEach(
+ integer -> System.out.println(integer)
+ );
+ }
+
+
+ class Solution {
+
+ List res = new LinkedList();
+ public List preorderTraversal(TreeNode root) {
+
+ if (root == null) {
+ return res;
+ }
+ return pre(root);
+ }
+
+ public List pre(TreeNode root) {
+ if (root == null) {
+ return null;
+ }
+ res.add(root.val);
+ pre(root.left);
+ pre(root.right);
+ return res;
+ }
+ }
+
+ class Solution1 {
+ public List preorderTraversal(TreeNode root) {
+ List res = new LinkedList();
+ if (root == null) {
+ return res;
+ }
+ // 基于指针
+ Deque queue = new LinkedList<>();
+ // 加入根节点
+ TreeNode index = root;
+ queue.push(index);
+ while (!queue.isEmpty()) {
+ index = queue.pop();
+ res.add(index.val);
+ // 先加入右子树,因为是栈,后出右边
+ if (index.right != null) {
+ queue.push(index.right);
+ }
+ // 后加入左子树
+ if (index.left != null) {
+ queue.push(index.left);
+ }
+ }
+ return res;
+ }
+
+ }
+
+
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode145.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode145.java
new file mode 100644
index 0000000..c968c3d
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode145.java
@@ -0,0 +1,77 @@
+package cn.whaifree.leetCode.Tree;
+
+import cn.whaifree.leetCode.model.TreeNode;
+import org.junit.Test;
+
+import java.util.*;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/1/15 20:11
+ * @注释
+ */
+public class LeetCode145 {
+
+ @Test
+ public void test() {
+ TreeNode root = TreeNode.constructTree(new Integer[]{1, 2, 3, 4, 5});
+
+ root.printTree();
+ new Solution1().postorderTraversal(root).forEach(
+ System.out::println
+ );
+ }
+
+ class Solution {
+ List res = new LinkedList<>();
+
+ public List postorderTraversal(TreeNode root) {
+ if (root == null) {
+ return res;
+ }
+ postorder(root);
+ return res;
+ }
+
+ public void postorder(TreeNode root) {
+ if (root==null) return;
+
+ postorder(root.left);
+ postorder(root.right);
+ res.add(root.val);
+
+ }
+ }
+
+
+ class Solution1 {
+ public List postorderTraversal(TreeNode root) {
+
+ List res = new LinkedList();
+ if (root == null) {
+ return res;
+ }
+ Deque stack = new ArrayDeque<>();
+
+ stack.push(root);
+ while (!stack.isEmpty()) {
+
+ TreeNode pop = stack.pop();
+ res.add(pop.val);
+ if (pop.left != null) {
+ stack.push(pop.left);
+ }
+ if (pop.right != null) {
+ stack.push(pop.right);
+ }
+ }
+
+ Collections.reverse(res);
+
+ return res;
+ }
+
+ }
+
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode150.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode150.java
new file mode 100644
index 0000000..2f05a53
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode150.java
@@ -0,0 +1,90 @@
+package cn.whaifree.leetCode.Stack;
+
+import org.junit.Test;
+
+import java.util.Deque;
+import java.util.LinkedList;
+
+/**
+ * 逆转波兰表达式
+ */
+public class LeetCode150 {
+
+ @Test
+ public void test() {
+ System.out.println(new Solution1().evalRPN(new String[]{"2", "1", "+", "3", "*"}));
+ System.out.println(new Solution1().evalRPN(new String[]{"4", "13", "5", "/", "+"}));
+ }
+
+
+ class Solution {
+ public int evalRPN(String[] tokens) {
+
+ Deque stack = new LinkedList<>();
+
+ int index = 0;
+ while (index < tokens.length) {
+ String token = tokens[index];
+ if (token.equals("+")) {
+ Integer pop1 = Integer.parseInt(stack.pop());
+ Integer pop2 = Integer.parseInt(stack.pop());
+ stack.push(String.valueOf(pop1 + pop2));
+ } else if (token.equals("-")) { //避免多次判断
+ Integer pop1 = Integer.parseInt(stack.pop());
+ Integer pop2 = Integer.parseInt(stack.pop());
+ stack.push(String.valueOf(pop2 - pop1));
+ } else if (token.equals("*")) {
+ Integer pop1 = Integer.parseInt(stack.pop());
+ Integer pop2 = Integer.parseInt(stack.pop());
+ stack.push(String.valueOf(pop2 * pop1));
+ } else if (token.equals("/")) {
+ Integer pop1 = Integer.parseInt(stack.pop());
+ Integer pop2 = Integer.parseInt(stack.pop());
+ stack.push(String.valueOf(pop2 / pop1));
+ } else {
+ stack.push(token);
+ }
+ index++;
+ }
+ return Integer.parseInt(stack.pop());
+
+ }
+ }
+
+ class Solution1 {
+ public int evalRPN(String[] tokens) {
+
+ Deque stack = new LinkedList<>();
+
+ for (String token : tokens) {
+ switch (token) {
+ case "+":
+ int i = Integer.parseInt(stack.pop());
+ int j = Integer.parseInt(stack.pop());
+ stack.push(String.valueOf(i + j));
+ break;
+ case "-":
+ int k = Integer.parseInt(stack.pop());
+ int l = Integer.parseInt(stack.pop());
+ stack.push(String.valueOf(l - k));
+ break;
+ case "*":
+ int m = Integer.parseInt(stack.pop());
+ int n = Integer.parseInt(stack.pop());
+ stack.push(String.valueOf(n * m));
+ break;
+ case "/":
+ int o = Integer.parseInt(stack.pop());
+ int p = Integer.parseInt(stack.pop());
+ stack.push(String.valueOf(p / o));
+ break;
+ default:
+ stack.push(token);
+ break;
+ }
+ }
+ return Integer.parseInt(stack.pop());
+ }
+ }
+
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode151.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode151.java
new file mode 100644
index 0000000..d08ff27
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode151.java
@@ -0,0 +1,123 @@
+package cn.whaifree.leetCode.String;
+
+import org.junit.Test;
+
+import java.util.*;
+
+
+public class LeetCode151 {
+
+
+ // todo
+ @Test
+ public void test() {
+ String s = " a b cd ef g ";
+ System.out.println(new Solution2().reverseWords(s));
+
+
+ }
+
+ class Solution {
+ public String reverseWords(String s) {
+ List list = new LinkedList<>();
+ String[] split = s.split(" ");
+ for (String s1 : split) {
+ if (!(s1.equals("") || s1.equals(" "))) {
+ list.add(s1);
+ }
+ }
+ StringBuilder buffer = new StringBuilder();
+ int index = list.size() - 1;
+ while (index > 0) {
+ buffer.append(list.get(index--)+" ");
+ }
+ buffer.append(list.get(0));
+ return buffer.toString();
+ }
+
+ }
+
+ // 不要使用辅助空间,空间复杂度要求为O(1)
+ class Solution1 {
+ public String reverseWords(String s) {
+ StringBuilder stringBuilder = deleteSpace(s, ' ');
+ reverse(stringBuilder, 0, stringBuilder.length() - 1);
+ reverseWord(stringBuilder);
+ // 移除多余空格
+ // 反转char
+ // 反转每个字符
+ return stringBuilder.toString();
+ }
+
+ public void reverseWord(StringBuilder stringBuilder) {
+ int start = 0;
+ int index = 0;
+ while (index < stringBuilder.length() + 1) {
+ if (index == stringBuilder.length() || stringBuilder.charAt(index) == ' ') {
+ reverse(stringBuilder, start, index - 1);
+ start = index + 1;
+ }
+ index++;
+ }
+ }
+ public void reverse(StringBuilder str, int start, int end) {
+ while (start < end) {
+ char tmp = str.charAt(start);
+ str.setCharAt(start, str.charAt(end));
+ str.setCharAt(end, tmp);
+ end--;
+ start++;
+ }
+ }
+
+
+ /**
+ * 双指针移动元素,移除target
+ * - 参考LeetCode 27
+
+ * @param target
+ * @return
+ */
+ public StringBuilder deleteSpace(String s,char target) {
+
+ // 前后指针
+ int start = 0;
+ int end = s.length() - 1;
+ while (s.charAt(start) == target) start++;
+ while (s.charAt(end) == target) end--;
+ // 1. 删除首尾空格
+
+ StringBuilder stringBuilder = new StringBuilder();
+ while (start <= end) {
+ if (s.charAt(start) !=target){
+ stringBuilder.append(s.charAt(start) );
+ }else if (s.charAt(start) == target && stringBuilder.charAt(stringBuilder.length() - 1) != target) {
+ stringBuilder.append(s.charAt(start) );
+ }
+ start++;
+ }
+ // 2. 如果遇到空格,判断上一个是不是空格
+ // - 不是,入String
+ // - 是,跳过
+
+
+ return stringBuilder;
+ }
+
+ }
+
+
+ class Solution2{
+
+ public String reverseWords(String s) {
+ // 删除前后空白
+ String trim = s.trim();
+ String[] split = trim.split("\\s+");
+ List list = Arrays.asList(split);
+ Collections.reverse(list);
+ return String.join(" ", list);
+ }
+
+ }
+
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode155.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode155.java
new file mode 100644
index 0000000..e81aec1
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode155.java
@@ -0,0 +1,104 @@
+package cn.whaifree.leetCode.Stack;
+
+import org.junit.Test;
+
+import java.util.*;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/9/1 0:52
+ * @注释
+ */
+public class LeetCode155 {
+
+ @Test
+ public void test2() {
+ MinStack minStack = new MinStack();
+ // ["MinStack","push","push","push","getMin","pop","top","getMin"]
+ minStack.push(-2);
+ minStack.push(0);
+ minStack.push(-3);
+ System.out.println(minStack.getMin());
+ minStack.pop();
+ System.out.println(minStack.top());
+ System.out.println(minStack.getMin());
+ }
+
+ class MinStack {
+
+ static class Item{
+ Integer item;
+ Integer minV;
+
+ public Item(Integer item, Integer minV) {
+ this.item = item;
+ this.minV = minV;
+ }
+ }
+
+ Deque- itemStack = null;
+
+ public MinStack() {
+ itemStack = new ArrayDeque<>();
+ }
+
+ public void push(int val) {
+ // 找到当前最小值
+ Integer minNow = Math.min(getMin(), val);
+
+ Item item = new Item(val, minNow);
+ itemStack.push(item);
+ }
+
+ public void pop() {
+ itemStack.pop();
+ }
+
+ public int top() {
+ if (itemStack.isEmpty()) {
+ return Integer.MAX_VALUE;
+ }
+ return itemStack.peek().item;
+ }
+
+ public int getMin() {
+ if (itemStack.isEmpty()) {
+ return Integer.MAX_VALUE;
+ }
+ return itemStack.peek().minV;
+ }
+ }
+
+ /**
+ * Your MinStack object will be instantiated and called as such:
+ * MinStack obj = new MinStack();
+ * obj.push(val);
+ * obj.pop();
+ * int param_3 = obj.top();
+ * int param_4 = obj.getMin();
+ */
+ @Test
+ public void test() {
+ MinStack minStack = new MinStack();
+ // ["MinStack","push","push","push","getMin","pop","top","getMin"]
+ minStack.push(-2);
+ minStack.push(0);
+ minStack.push(-3);
+ System.out.println(minStack.getMin());
+ minStack.pop();
+ System.out.println(minStack.top());
+ System.out.println(minStack.getMin());
+ }
+
+
+
+/**
+ * Your MinStack object will be instantiated and called as such:
+ * MinStack obj = new MinStack();
+ * obj.push(val);
+ * obj.pop();
+ * int param_3 = obj.top();
+ * int param_4 = obj.getMin();
+ */
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode160.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode160.java
new file mode 100644
index 0000000..85664cf
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode160.java
@@ -0,0 +1,30 @@
+package cn.whaifree.leetCode.LinkedList;
+
+import cn.whaifree.leetCode.model.ListNode;
+import org.junit.Test;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/4/13 11:06
+ * @注释
+ */
+public class LeetCode160 {
+
+ @Test
+ public void test()
+ {
+
+ }
+
+ class Solution {
+ public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
+ ListNode A = headA, B = headB;
+ while (A != B) {
+ A = A == null ? headB : A.next;
+ B = B == null ? headA : B.next;
+ }
+ return A;
+ }
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode165.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode165.java
new file mode 100644
index 0000000..a96ee48
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode165.java
@@ -0,0 +1,103 @@
+package cn.whaifree.leetCode.String;
+
+import org.junit.Test;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/8/31 14:57
+ * @注释
+ */
+public class LeetCode165 {
+
+ @Test
+ public void test() {
+ Solution2 solution = new Solution2();
+ int result = solution.compareVersion("1.01.2", "1.001");
+ System.out.println(result);
+ }
+
+ class Solution2 {
+ /**
+ * 双指针,不断计算有效长度
+ * @param version1
+ * @param version2
+ * @return
+ */
+ public int compareVersion(String version1, String version2) {
+
+ int v1Index = 0;
+ int v2Index = 0;
+ while (v1Index < version1.length() || v2Index < version2.length()) {
+ int tmpV1Sum = 0;
+ while (v1Index < version1.length() && version1.charAt(v1Index) != '.') {
+ tmpV1Sum += tmpV1Sum * 10 + version1.charAt(v1Index) - '0';
+ v1Index++;
+ }
+ v1Index++; // 跳过.
+
+ int tmpV2Sum = 0;
+ while (v2Index < version2.length() && version2.charAt(v2Index) != '.') {
+ tmpV2Sum += tmpV2Sum * 10 + version2.charAt(v2Index) - '0';
+ v2Index++;
+ }
+ v2Index++; // 跳过.
+ if (tmpV1Sum < tmpV2Sum) {
+ return -1;
+ } else if (tmpV1Sum > tmpV2Sum) {
+ return 1;
+ }
+ }
+ return 0;
+ }
+ }
+
+ class Solution {
+ /**
+ * 1.2 1.10
+ * 1.01 1.001
+ * 1.0.0.0
+ *
+ *
+ * @param version1
+ * @param version2
+ * @return
+ */
+ public int compareVersion(String version1, String version2) {
+ String[] splitV1 = version1.split("\\.");
+ String[] splitV2 = version2.split("\\.");
+ if (splitV1.length < splitV2.length) {
+ splitV1 = fill(splitV1, splitV2.length);
+ }else {
+ splitV2 = fill(splitV2, splitV1.length);
+ }
+ // 现在两边一样长了
+
+ for (int i = 0; i < splitV1.length; i++) {
+ Integer v1 = Integer.valueOf(splitV1[i]);
+ Integer v2 = Integer.valueOf(splitV2[i]);
+ if (v1 > v2) {
+ return 1;
+ } else if (v1 < v2) {
+ return -1;
+ }
+ }
+
+ return 0;
+
+
+ }
+
+ public String[] fill(String[] split, int newLen) {
+ String[] tmp = new String[newLen];
+ int i = 0;
+ for (; i < split.length; i++) {
+ tmp[i] = split[i];
+ }
+ for (; i < tmp.length; i++) {
+ tmp[i] = "0";
+ }
+ return tmp;
+ }
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode167.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode167.java
new file mode 100644
index 0000000..7bd91cd
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode167.java
@@ -0,0 +1,68 @@
+package cn.whaifree.leetCode.Array;
+
+import org.junit.Test;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/9/19 23:02
+ * @注释
+ */
+public class LeetCode167 {
+
+ @Test
+ public void test() {
+
+ Solution1 solution = new Solution1();
+ int[] ints = solution.twoSum(new int[]{2,7,11,15}, 9);
+ System.out.println(ints[0] + " " + ints[1]);
+ }
+
+ class Solution {
+ public int[] twoSum(int[] numbers, int target) {
+
+ for (int i = 0; i < numbers.length; i++) {
+ int tar = target - numbers[i];
+ int j = binarySearch(numbers, tar);
+ if (j != -1 && i != j) {
+ return i < j ? new int[]{i + 1, j + 1} : new int[]{j + 1, i + 1};
+ }
+ }
+ return null;
+ }
+
+ public int binarySearch(int[] nums, int target) {
+ int left = 0;
+ int right = nums.length - 1;
+ while (left <= right) {
+ int mid = (left + right) / 2;
+ if (nums[mid] == target) {
+ return mid;
+ } else if (nums[mid] < target) {
+ left = mid + 1;
+ } else {
+ right = mid - 1;
+ }
+ }
+ return -1;
+ }
+ }
+
+ class Solution1 {
+ public int[] twoSum(int[] numbers, int target) {
+ int left = 0;
+ int right = numbers.length - 1;
+ while (left < right) {
+ int sum = numbers[left] + numbers[right];
+ if (sum == target) {
+ return new int[]{left + 1, right + 1};
+ } else if (sum < target) {
+ left++;
+ }else {
+ right--;
+ }
+ }
+ return null;
+ }
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode169.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode169.java
new file mode 100644
index 0000000..5fe0bf3
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode169.java
@@ -0,0 +1,57 @@
+package cn.whaifree.leetCode.Array;
+
+import org.junit.Test;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/9/1 2:21
+ * @注释
+ */
+public class LeetCode169
+{
+ @Test
+ public void test()
+ {
+
+ int[] nums = {2,2,1,1,1,2,2};
+ Solution solution = new Solution();
+ int i = solution.majorityElement(nums);
+ System.out.println(i);
+ }
+ class Solution {
+ /**
+ * 遍历每个元素
+ * count 为现在出现最多的数
+ * - 如果count = 0 (表示发生了是和不是的变化)
+ * - 这个元素出现的次数>=之前元素出现的个数
+ * 所以基准变为这个元素
+ * count = item == base : 1:-1
+ *
+ * 如果是这个元素+1,不是这个元素-1
+ *
+ * cge5 cge5 cge7
+ * 1 2 1 2 1 0 1 0 1 2 1 0 1 2 3 4
+ * [7, 7, 5, 7, 5, 1 | 5, 7 | 5, 5, 7, 7 | 7, 7, 7, 7]
+ * 在遍历到数组中的第一个元素以及每个在 | 之后的元素时,candidate 都会因为 count 的值变为 0 而发生改变。最后一次 candidate 的值从 5 变为 7,也就是这个数组中的众数。
+ *
+ *
+ * @param nums
+ * @return
+ */
+ public int majorityElement(int[] nums) {
+ int count = 0;
+ Integer candidate = null;
+
+ for (int num : nums) {
+ if (count == 0) {
+ candidate = num;
+ System.out.println("基准元素:" + candidate);
+ }
+ count += (num == candidate) ? 1 : -1;
+ }
+
+ return candidate;
+ }
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode17.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode17.java
new file mode 100644
index 0000000..c829ace
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode17.java
@@ -0,0 +1,119 @@
+package cn.whaifree.leetCode.BackTracking;
+
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/2/2 10:31
+ * @注释
+ */
+public class LeetCode17 {
+
+ @Test
+ public void test() {
+ String digits = "23";
+ new Solution().letterCombinations(digits).forEach(s -> System.out.println(s));
+ }
+
+ class Solution {
+ List map = new ArrayList<>();
+ List result = new ArrayList<>();
+ // 记录 路径
+ StringBuilder s = new StringBuilder();
+
+ public List letterCombinations(String digits) {
+ if (digits.length() == 0) {
+ return result;
+ }
+ map.add(0, null);
+ map.add(1, null);
+ map.add(2, "abc");
+ map.add(3, "def");
+ map.add(4, "ghi");
+ map.add(5, "jkl");
+ map.add(6, "mno");
+ map.add(7, "pqrs");
+ map.add(8, "tuv");
+ map.add(9, "wxyz");
+ backTracking(digits, 0, digits.length());
+ return result;
+ }
+
+ /**
+ *
+ * @param digits 原始字符 23
+ * @param number 使用到第几个字符
+ * @param needLength 需要几个字符,等价于digits.length
+ */
+ void backTracking(String digits, int number,int needLength) {
+ if (s.length() == needLength) {
+ result.add(new String(s.toString()));
+ return;
+ }
+
+ int c = digits.charAt(number) - 48;
+ String sValue = map.get(c);
+ int length = sValue.length();
+ // 对每个字符处理
+ for (int i = 0; i < length; i++) {
+ char c1 = sValue.charAt(i);
+ s.append(c1);
+ backTracking(digits, number + 1, needLength);
+ s.deleteCharAt(s.length() - 1);
+ }
+ }
+ }
+
+ class Solution1 {
+ List map = new ArrayList<>();
+ List result = new ArrayList<>();
+ // 记录 路径
+ StringBuilder s = new StringBuilder();
+
+ public List letterCombinations(String digits) {
+ if (digits.length() == 0) {
+ return result;
+ }
+ map.add(0, null);
+ map.add(1, null);
+ map.add(2, "abc");
+ map.add(3, "def");
+ map.add(4, "ghi");
+ map.add(5, "jkl");
+ map.add(6, "mno");
+ map.add(7, "pqrs");
+ map.add(8, "tuv");
+ map.add(9, "wxyz");
+
+ backTracking(digits, 0);
+ return result;
+ }
+
+ /**
+ *
+ * @param digits 原始字符 23
+ * @param number 使用到第几个字符
+ */
+ void backTracking(String digits, int number) {
+ if (s.length() == digits.length()) {
+ result.add(new String(s.toString()));
+ return;
+ }
+
+ int c = digits.charAt(number) - 48;
+ String sValue = map.get(c);
+ int length = sValue.length();
+ // 对每个字符处理
+ for (int i = 0; i < length; i++) {
+ char c1 = sValue.charAt(i);
+ s.append(c1);
+ backTracking(digits, number + 1);
+ s.deleteCharAt(s.length() - 1);
+ }
+ }
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode173.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode173.java
new file mode 100644
index 0000000..042089e
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode173.java
@@ -0,0 +1,128 @@
+package cn.whaifree.leetCode.LinkedList;
+
+import cn.whaifree.leetCode.model.TreeNode;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Deque;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/10/27 22:42
+ * @注释
+ */
+public class LeetCode173 {
+
+ class BSTIterator1 {
+
+ List list;
+ Integer index = -1;
+
+ public BSTIterator1(TreeNode root) {
+ list = new ArrayList<>();
+ inOrderTraversal(root);
+ }
+
+ public int next() {
+ index++;
+ return list.get(index);
+ }
+
+ public boolean hasNext() {
+ return index < list.size() - 1;
+ }
+
+ public void inOrderTraversal(TreeNode root) {
+ if (root == null) {
+ return;
+ }
+ inOrderTraversal(root.left);
+ list.add(root.val);
+ inOrderTraversal(root.right);
+ }
+ }
+
+ @Test
+ public void test1() {
+ TreeNode root = TreeNode.constructTree(new Integer[]{7, 3, 15, null, null, 9, 20});
+ BSTIterator bstIterator = new BSTIterator(root);
+ while (bstIterator.hasNext()) {
+ System.out.println(bstIterator.next());
+ }
+ }
+
+ class BSTIterator {
+ /**
+ * next访问到某个节点,就让他右边入栈
+ */
+
+ Deque stack;
+ public BSTIterator(TreeNode root) {
+ // 每层放左边第一个
+ stack = new LinkedList<>();
+ TreeNode index = root;
+ while (index != null) {
+ stack.push(index);
+ index = index.left;
+ }
+ }
+
+ public int next() {
+ TreeNode pop = stack.pop(); // 当前指针位置
+ if (pop.right != null) { // 如果有边有,就把右边和右边的左子树都放入栈中
+ TreeNode index = pop.right;
+ while (index != null) {
+ stack.push(index);
+ index = index.left;
+ }
+ }
+ return pop.val;
+ }
+
+ public boolean hasNext() {
+ return !stack.isEmpty();
+ }
+ }
+
+
+ class BSTIterator2 {
+ /**
+ * next访问到某个节点,就让他右边入栈
+ */
+
+ Deque stack;
+ TreeNode now;
+ public BSTIterator2(TreeNode root) {
+ // 每层放左边第一个
+ stack = new LinkedList<>();
+ now = root;
+ }
+
+ public int next() {
+ while (now != null) {
+ stack.push(now);
+ now = now.left;
+ }
+ now = stack.pop();
+ int val = now.val;
+ now = now.right;
+ return val;
+ }
+
+ public boolean hasNext() {
+ return now != null ||!stack.isEmpty();
+ }
+ }
+
+/**
+ * Your BSTIterator object will be instantiated and called as such:
+ * BSTIterator obj = new BSTIterator(root);
+ * int param_1 = obj.next();
+ * boolean param_2 = obj.hasNext();
+ */
+}
+
+
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode17_12BiNode.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode17_12BiNode.java
new file mode 100644
index 0000000..0b9eba0
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode17_12BiNode.java
@@ -0,0 +1,68 @@
+package cn.whaifree.leetCode.Tree;
+
+import cn.whaifree.leetCode.model.TreeNode;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/1/28 19:41
+ * @注释
+ */
+public class LeetCode17_12BiNode {
+
+ @Test
+ public void test() {
+ TreeNode root = TreeNode.constructTreeByArray(4, 2, 5, 1, 3, null, 6, 0);
+ TreeNode treeNode = new Solution().convertBiNode(root);
+ TreeNode.treeToArray(treeNode).forEach(i -> System.out.println(i));
+
+ }
+ class Solution {
+
+ TreeNode res = new TreeNode();
+ TreeNode index = res;
+ public TreeNode convertBiNode(TreeNode root) {
+ circle(root);
+ return res.right;
+ }
+
+ private void circle(TreeNode root) {
+ if (root == null) {
+ return;
+ }
+ circle(root.left);
+ index.right = new TreeNode(root.val);
+ index = index.right;
+ circle(root.right);
+ }
+ }
+
+ class Solution1 {
+
+ TreeNode res = new TreeNode();
+ TreeNode index = res;
+ public TreeNode convertBiNode(TreeNode root) {
+ circle(root);
+ return res.right;
+ }
+
+ private void circle(TreeNode root) {
+ if (root == null) {
+ return;
+ }
+ circle(root.left);
+ // 直接使用,不创建新对象,能提升很多效率
+ index.right = root;
+ root.left = null;
+ index = index.right;
+ circle(root.right);
+ }
+ }
+}
+
+
+
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode189.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode189.java
new file mode 100644
index 0000000..d1edec6
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode189.java
@@ -0,0 +1,58 @@
+package cn.whaifree.leetCode.Array;
+
+import org.junit.Test;
+
+import java.util.Arrays;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/9/16 12:15
+ * @注释
+ */
+public class LeetCode189 {
+ @Test
+ public void test()
+ {
+ int[] nums = new int[]{-1};
+ new Solution1().rotate(nums, 3);
+ System.out.println(Arrays.toString(nums));
+ }
+
+
+
+ class Solution {
+ public void rotate(int[] nums, int k) {
+ int[] newNums = new int[nums.length];
+ for (int i = 0; i < newNums.length; i++) {
+ newNums[(i + k) % nums.length] = nums[i];
+ }
+ System.arraycopy(newNums, 0, nums, 0, nums.length);
+
+ }
+
+ }
+
+ class Solution1 {
+ public void rotate(int[] nums, int k) {
+ k %= nums.length;
+ reverse(nums, 0, nums.length - 1);
+ reverse(nums, 0, k - 1);
+ reverse(nums, k, nums.length - 1);
+ }
+
+ public void reverse(int[] nums, int start, int end) {
+ while (start < end) {
+ swap(nums, start, end);
+ start++;
+ end--;
+ }
+ }
+
+ public void swap(int[] nums, int i, int j) {
+ int temp = nums[i];
+ nums[i] = nums[j];
+ nums[j] = temp;
+ }
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode19.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode19.java
new file mode 100644
index 0000000..75a1f8f
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode19.java
@@ -0,0 +1,144 @@
+package cn.whaifree.leetCode.LinkedList;
+
+import cn.whaifree.leetCode.model.ListNode;
+import org.junit.Test;
+
+import java.util.Deque;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Stack;
+
+public class LeetCode19 {
+
+ @Test
+ public void test() {
+ Solution2 solution = new Solution2();
+ ListNode listNode = solution.removeNthFromEnd(ListNode.listNodeFromArray(new int[]{1,2,3,4}), 3);
+ ListNode.printList(listNode);
+
+ }
+
+ /**
+ * Definition for singly-linked list.
+ * public class ListNode {
+ * int val;
+ * ListNode next;
+ * ListNode() {}
+ * ListNode(int val) { this.val = val; }
+ * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
+ * }
+ */
+ class Solution {
+
+
+ /**
+ * 链表中结点的数目为 sz
+ * 1 <= sz <= 30
+ * 0 <= Node.val <= 100
+ * 1 <= n <= sz
+ *
+ * 时间复杂度:O(Length)
+ * 空间复杂度:O(1)
+ *
+ * @param head
+ * @param n
+ * @return
+ */
+ public ListNode removeNthFromEnd(ListNode head, int n) {
+
+ // 空链表
+ if (head == null) {
+ return head;
+ }
+ int length = getLength(head);
+ // 删除头
+ if (n == length) {
+ head = head.next;
+ return head;
+ }
+
+ int find = length - n;
+ ListNode index = head;
+ for (int i = 0; i < find - 1; i++) {
+ index = index.next;
+ }
+ index.next = index.next.next;
+ return head;
+ }
+
+ int getLength(ListNode head) {
+ int length = 0;
+ ListNode index = head;
+ while (index != null) {
+ index = index.next;
+ length++;
+ }
+ return length;
+ }
+ }
+
+
+ class Solution1 {
+
+
+ /**
+ * 栈
+ * 时间复杂度:O(Length)
+ * 空间复杂度:O(Length)
+ *
+ * 修改多使用这个栈会更快
+ * LinkedList比Stack快的原因如下1:
+ * 基于数组实现:Stack基于数组实现,随机访问(查找)效率更高,增删改效率较低。
+ * 基于链表实现:LinkedList基于链表实现,增删改效率更高,随机访问(查找)效率较低。
+ * 对于频繁的插入、删除操作,利用LinkedList实现栈自然比Stack快很多。
+ * @param head
+ * @param n
+ * @return
+ */
+ public ListNode removeNthFromEnd(ListNode head, int n) {
+
+ ListNode dummy = new ListNode(0, head);
+ Deque stack = new LinkedList();
+ // 双端队列
+ ListNode index = dummy;
+ while (index != null) {
+ stack.push(index);
+ index = index.next;
+ }
+ for (int i = 0; i < n; i++) {
+ stack.pop();
+ }
+ ListNode peek = stack.peek();
+ peek.next = peek.next.next;
+ return dummy.next;
+ }
+ }
+
+
+ class Solution2 {
+
+
+ /**
+ * 双指针 快慢节点追逐:
+ * 两个节点的差值为n,快指针走完了,慢指针就是了
+ *
+ * @param head
+ * @param n
+ * @return
+ */
+ public ListNode removeNthFromEnd(ListNode head, int n) {
+ ListNode dummy = new ListNode(0, head);
+ ListNode fast = dummy;
+ ListNode slow = dummy;
+ for (int i = 0; i <= n; i++) {
+ fast = fast.next;
+ }
+ while (fast!= null) {
+ fast = fast.next;
+ slow = slow.next;
+ }
+ slow.next = slow.next.next;
+ return dummy.next;
+ }
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode198.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode198.java
new file mode 100644
index 0000000..6e3210c
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode198.java
@@ -0,0 +1,40 @@
+package cn.whaifree.leetCode.Dynamic;
+
+import org.junit.Test;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/4/3 13:50
+ * @注释
+ */
+public class LeetCode198 {
+
+ @Test
+ public void test()
+ {
+ Solution solution = new Solution();
+ int[] nums = new int[]{2,7};
+ int rob = solution.rob(nums);
+ System.out.println(rob);
+ }
+
+ class Solution {
+ public int rob(int[] nums) {
+ if (nums.length == 1) {
+ return nums[0];
+ }
+ // dp[i] 表示 从0-i偷窃都最大价值
+ // dp[i] = max(dp[i-1],dp[i-2]+nums[i]])
+ int[] dp = new int[nums.length];
+ dp[0] = nums[0];
+ dp[1] = Math.max(nums[0], nums[1]);
+ for (int i = 2; i < nums.length; i++) {
+ dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);
+ }
+ return dp[nums.length - 1];
+ }
+ }
+
+
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode199.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode199.java
new file mode 100644
index 0000000..9b4d999
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode199.java
@@ -0,0 +1,87 @@
+package cn.whaifree.leetCode.Tree;
+
+import cn.whaifree.leetCode.model.TreeNode;
+import org.junit.Test;
+
+import java.util.*;
+import java.util.function.Consumer;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/1/17 15:07
+ * @注释
+ */
+public class LeetCode199 {
+
+ @Test
+ public void test() {
+ TreeNode root = TreeNode.constructTree(new Integer[]{1, 2, 3, 4});
+ root.printTree();
+ System.out.println(new Solution1().rightSideView(root));
+ }
+
+
+
+ class Solution {
+ /**
+ * 层次遍历,每层最右边就是结果
+ * @param root
+ * @return
+ */
+ public List rightSideView(TreeNode root) {
+ List res = new ArrayList<>();
+ if (root == null) {
+ return res;
+ }
+
+ Deque queue = new LinkedList<>();
+
+ queue.add(root);
+ while (!queue.isEmpty()) {
+ int size = queue.size();
+ for (int i = 0; i < size; i++) {
+ TreeNode pop = queue.pop();
+ // 只加入每层最后一个元素
+ if(i==size-1) res.add(pop.val);
+ if (pop.left != null) {
+ queue.add(pop.left);
+ }
+ if (pop.right != null) {
+ queue.add(pop.right);
+ }
+ }
+ }
+ return res;
+ }
+ }
+
+ class Solution1 {
+
+
+ List res = new ArrayList<>();
+ public List rightSideView(TreeNode root) {
+ if (root == null) {
+ return res;
+ }
+ level(root, 0);
+ return res;
+ }
+
+ public void level(TreeNode root, int level) {
+
+ if (root == null) {
+ return;
+ }
+
+ // 每层只有一个输出,就是每层最右边的那个
+ // 每层都是让右边先进入递归,就能保证获取到最右的数据
+ if (res.size() == level) {
+ res.add(root.val);
+ }
+ level(root.right, level + 1);
+ level(root.left, level + 1);
+
+ }
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode2.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode2.java
new file mode 100644
index 0000000..f4500f2
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode2.java
@@ -0,0 +1,121 @@
+package cn.whaifree.leetCode.LinkedList;
+
+import cn.whaifree.leetCode.model.ListNode;
+import org.junit.Test;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/4/20 11:42
+ * @注释
+ */
+public class LeetCode2 {
+
+ @Test
+ public void test() {
+ new Solution1().addTwoNumbers(
+ /*[9,9,9,9,9,9,9], l2 = [9,9,9,9]*/
+ ListNode.listNodeFromArray(new int[]{9, 9, 9, 9, 9, 9, 9})
+ , ListNode.listNodeFromArray(new int[]{9, 9, 9, 9})
+ ).printList();
+ }
+
+ class Solution {
+ public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
+ return circle(l1, l2, false);
+ }
+
+ /**
+ *
+ * @param l1
+ * @param l2
+ * @param in 是否进1
+ * @return
+ */
+ public ListNode circle(ListNode l1, ListNode l2, boolean in) {
+
+ if (l1 == null && l2 == null && in) {
+ return new ListNode(1);
+ }
+
+ if (l1 == null) {
+ // l2 调用另一个递归方法,不断去判断是否需要在当前节点进1
+ return judgeIn(l2, in);
+ } else if (l2 == null) {
+ return judgeIn(l1, in);
+ }
+
+ int val = 0;
+ int sum = l1.val + l2.val;
+ if (in) {
+ val = sum + 1;
+ }else {
+ val = sum;
+ }
+
+ ListNode res = new ListNode(val % 10);
+ res.next = circle(l1.next, l2.next, val >= 10);
+ return res;
+ }
+
+ public ListNode judgeIn(ListNode node, boolean in) {
+ if (node == null) {
+ if (in) {
+ return new ListNode(1);
+ }
+ return null;
+ }
+
+ int val = 0;
+ if (in) {
+ val = node.val + 1;
+ }else {
+ val = node.val;
+ }
+ ListNode res = new ListNode(val % 10);
+ res.next = judgeIn(node.next, val >= 10);
+ return res;
+ }
+ }
+
+ class Solution1 {
+ public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
+ ListNode indexA = l1;
+ ListNode indexB = l2;
+ boolean flagIn = false;
+ while (indexA != null && indexB != null) {
+ int sum = indexA.val + indexB.val;
+ if (flagIn) {
+ sum += 1;
+ }
+ indexA.val = sum % 10;
+ if (sum >= 10) {
+ flagIn = true;
+ }else {
+ flagIn = false;
+ }
+
+ if (indexA.next == null && indexB.next == null && flagIn) {
+ indexA.next = new ListNode(1);
+ break;
+ }
+
+ if (indexA.next == null && indexB.next != null) {
+ indexA.next = new ListNode(0);
+ }
+ if (indexB.next == null && indexA.next != null) {
+ indexB.next = new ListNode(0);
+ }
+
+
+ indexB = indexB.next;
+ indexA = indexA.next;
+ }
+
+
+ return l1;
+ }
+
+ }
+
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode20.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode20.java
new file mode 100644
index 0000000..06085ee
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode20.java
@@ -0,0 +1,46 @@
+package cn.whaifree.leetCode.Stack;
+
+import org.junit.Test;
+
+import java.util.Deque;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Stack;
+
+/**
+ * 括号匹配
+ */
+public class LeetCode20 {
+
+ @Test
+ public void test() {
+ System.out.println(new Solution().isValid("()[]{}"));
+ System.out.println(new Solution().isValid("}"));
+ }
+
+ class Solution {
+ public boolean isValid(String s) {
+ Deque stack = new LinkedList<>();
+ char[] chars = s.toCharArray();
+ for (char aChar : chars) {
+ if (aChar == '{' || aChar == '[' || aChar == '(') {
+ stack.push(aChar);
+ }else if (aChar == ']'){
+ if (stack.isEmpty() || stack.pop() != '[') {
+ return false;
+ }
+ }else if (aChar == '}'){
+ if (stack.isEmpty() ||stack.pop()!= '{') {
+ return false;
+ }
+ }else if (aChar == ')'){
+ if (stack.isEmpty() || stack.pop() != '(') {
+ return false;
+ }
+ }
+ }
+
+ return stack.isEmpty();
+ }
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode203.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode203.java
new file mode 100644
index 0000000..e2c8b8f
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode203.java
@@ -0,0 +1,211 @@
+package cn.whaifree.leetCode.LinkedList;
+
+
+import cn.whaifree.leetCode.model.ListNode;
+import org.junit.Test;
+
+/**
+ * 链表
+ *
+ * 203. 移除链表元素
+ * 示例 1:
+
+ * 输入:head = [1,2,6,3,4,5,6], val = 6
+ * 输出:[1,2,3,4,5]
+ * 示例 2:
+ *
+ * 输入:head = [], val = 1
+ * 输出:[]
+ * 示例 3:
+ *
+ * 输入:head = [7,7,7,7], val = 7
+ * 输出:[]
+ *
+ */
+public class LeetCode203 {
+
+
+// /**
+// * [6] 6
+// * [] 6
+// * [2] 6
+// *
+// *
+// * @param head
+// * @param val
+// * @return
+// */
+// public ListNode removeElements(ListNode head, int val) {
+//
+// // 如果链表只有一个节点或者没有要删除的元素,则直接返回原链表
+// if (head == null) {
+// return head;
+// }
+// if (head.next == null && head.val == val) {
+// head = null;
+// return head;
+// } else if (head.next == null) {
+// return head;
+// }
+//
+// // 定义一个指针pre指向head节点
+// // 定义一个指针index指向head.next节点
+// ListNode pre = head;
+// ListNode index = head.next;
+//
+// // 遍历链表,直到index.next为null
+// while (index.next != null) {
+//
+// // 如果index节点的值等于要删除的元素val
+// if (index.val == val) {
+//
+// // 删除该节点
+// pre.next = index.next;
+//
+// // 将指针index移动到下一个节点
+// index = pre.next;
+//
+// // 将指针pre移动到下一个节点
+// pre = pre.next;
+//
+// // 继续遍历链表
+// continue;
+// }
+//
+// // 如果index节点的值不等于要删除的元素val
+// index = index.next;
+// pre = pre.next;
+// }
+// // 尾巴节点为val,那就删除尾巴节点
+// if (index.val == val) {
+// pre.next = null;
+// }
+// // 如果头节点的值等于要删除的元素val
+// if (head.val == val) {
+// head = head.next;
+// }
+//
+//
+// // 返回删除元素后的链表
+// return head;
+// }
+
+
+ public ListNode removeElements1(ListNode head, int val) {
+ // 找到第一个head不删除的点 删除头结点时另做考虑
+ while (head != null && head.val == val) {
+ head = head.next;
+ }
+ // 保证删完后不为空
+ if(head==null)
+ return head;
+ //
+ ListNode index = head;
+
+ while (index.next != null) {
+ if (index.next.val == val) {
+ index.next = index.next.next;
+ } else {
+ index = index.next;
+ }
+ }
+ return head;
+ }
+
+// /**
+// * 递归
+// * @param head
+// * @param val
+// * @return
+// */
+// public ListNode removeElements(ListNode head, int val) {
+// if (head == null) {
+// return null;
+// }
+// head.next = removeElements(head.next, val);
+// if (head.next.val == val) {
+// return head.next.next;
+// } else {
+// return head.next;
+// }
+// }
+
+
+ /**
+ * 递归
+ * 1. 停止条件
+ * 2. 循环返回值
+ * @param head
+ * @param val
+ * @return
+ */
+ public ListNode removeElements(ListNode head, int val) {
+ if (head == null) {
+ return null;
+ }
+ // h
+ // 6 6
+ // 让head 之后的元素交给下个递归
+ head.next = removeElements(head.next, val);
+ if (head.val == val) {
+ return head.next;
+ } else {
+ return head;
+ }
+
+ // 递归的返回值为head.next,即传入的下一结点;
+ // 如果匹配就返回当前结点;不匹配,返回的head就是前一结点了。
+ // 压栈时的head.next为后一个结点;弹栈时的head.next就位后前一个结点
+ }
+
+ public ListNode removeElements3(ListNode head, int val) {
+ if (head == null) {
+ return head;
+ }
+
+ // 1. 获取当前节点
+ // 2. 递归next 6 6
+ head.next = removeElements3(head.next, val);
+ if (head.val == val) {
+ return head.next;
+ } else {
+ return head;
+ }
+ }
+
+
+ /**
+ * 0 1 2 3
+ * 加上虚拟表头节点
+ * @param head
+ * @param val
+ * @return
+ */
+ public ListNode removeElements7(ListNode head, int val) {
+ ListNode virtualHead = new ListNode(0, head);
+ ListNode pre = virtualHead;
+ ListNode index = head;
+ head = virtualHead;
+ while (index != null) {
+ if (index.val == val) {
+ pre.next = index.next;
+ index = pre.next;
+ }else {
+ pre = pre.next;
+ index = index.next;
+ }
+ }
+ return head.next;
+ }
+
+
+
+
+ @Test
+ public void Test() {
+ ListNode listNode = ListNode.listNodeFromArray(new int[]{7,7,7,7});
+// ListNode.printList(listNode);
+ ListNode listNode1 = removeElements7(listNode, 7);
+ ListNode.printList(listNode1);
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode206.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode206.java
new file mode 100644
index 0000000..3f3eeae
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode206.java
@@ -0,0 +1,127 @@
+package cn.whaifree.leetCode.LinkedList;
+
+import cn.whaifree.leetCode.model.ListNode;
+
+import org.junit.Test;
+
+import java.util.List;
+import java.util.Queue;
+import java.util.Stack;
+
+/**
+ * 206. 反转链表
+ * 给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。
+ *
+ * 示例 1:
+ * 输入:head = [1,2,3,4,5]
+ * 输出:[5,4,3,2,1]
+ *
+ * 示例 2:
+ * 输入:head = [1,2]
+ * 输出:[2,1]
+ * 示例 3:
+ * 输入:head = []
+ * 输出:[]
+
+ * 提示:
+ *
+ * 链表中节点的数目范围是 [0, 5000]
+ * -5000 <= Node.val <= 5000
+ */
+public class LeetCode206 {
+
+ @Test
+ public void test() {
+ ListNode listNode = ListNode.listNodeFromArray(new int[]{1,2,3,4,5});
+ ListNode listNode1 = reverseList2(listNode);
+ ListNode.printList(listNode1);
+ }
+
+ /**
+ * Definition for singly-linked list.
+ * public class ListNode {
+ * int val;
+ * ListNode next;
+ * ListNode() {}
+ * ListNode(int val) { this.val = val; }
+ * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
+ * }
+ */
+
+ /**
+ * 使用栈
+ * @param head
+ * @return
+ */
+ public ListNode reverseList(ListNode head) {
+ Stack integerStack = new Stack<>();
+ ListNode index = head;
+ while (index!= null) {
+ integerStack.add(index.val);
+ index = index.next;
+ }
+
+ ListNode ansHead = new ListNode();
+ index = ansHead;
+ while (!integerStack.empty()) {
+ index.next = new ListNode(integerStack.pop());
+ index = index.next;
+ }
+ return ansHead.next;
+ }
+
+
+ /**
+ *
+ *
+ * 使用三个指针 pre index tmp
+ * - 存储tmp为index的下一个节点,让index指向pre。
+ * pre index tmp
+ * 1 <-- 2 3 --> 4
+ * pre index tmp
+ * 1 <-- 2 3 --> 4
+ * pre index tmp
+ * 1 <-- 2 <-- 3 4
+ *
+ * 移动指针不一定要index.next, 在指针固定顺序的时候
+ * 可以让tmp=index.next;pre=index;index=tmp
+ *
+ * @param head
+ * @return
+ */
+ public ListNode reverseList1(ListNode head) {
+ ListNode pre = null;
+ ListNode tmp = null;
+ ListNode index = head;
+ while (index != null) {
+ tmp = index.next;
+ index.next = pre;
+ pre = index;
+ index = tmp;
+ }
+ return pre;
+ }
+
+
+ /**
+ * 递归
+ * - 递归 只考虑当前这个部分,把其他的部分扔给下一次递归
+ * @param head
+ * @return
+ */
+ public ListNode reverseList2(ListNode head) {
+ // 两个逆转
+ return reverse(null, head);
+ }
+
+ public ListNode reverse(ListNode pre, ListNode cur) {
+ if (cur == null) {
+ return pre;
+ }
+ // 只考虑三个点,1 2逆转,3(tmp 2.next)为下次递归的输入
+ ListNode tmp = cur.next;
+ cur.next = pre;
+ return reverse(cur, tmp);
+ }
+
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode207.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode207.java
new file mode 100644
index 0000000..2cb2142
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode207.java
@@ -0,0 +1,100 @@
+package cn.whaifree.leetCode.Graph;
+
+import org.junit.Test;
+
+import java.util.*;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/9/16 0:02
+ * @注释
+ */
+public class LeetCode207 {
+
+ @Test
+ public void test() {
+ int numCourses = 4;
+ int[][] prerequisites = {{1, 0},{0,2},{1,3}};
+ System.out.println(new Solution().canFinish(numCourses, prerequisites));
+ }
+ class Solution {
+ public boolean canFinish(int numCourses, int[][] prerequisites) {
+ List> graph = new ArrayList<>();
+
+ for (int i = 0; i < numCourses; i++) {
+ graph.add(new ArrayList<>());
+ }
+
+ // 统计入度个数
+ int[] inGre = new int[numCourses];
+ for (int i = 0; i < prerequisites.length; i++) {
+ int course = prerequisites[i][0];
+ int preCourse = prerequisites[i][1];
+ graph.get(preCourse).add(course);
+ inGre[course]++;
+ }
+
+ // 对所有入度为0的进入队列
+ Deque queue = new ArrayDeque<>();
+ for (int i = 0; i < numCourses; i++) {
+ if (inGre[i] == 0) {
+ queue.add(i);
+ }
+ }
+ // 出对,并去边
+ int exeCount = 0;
+ while (!queue.isEmpty()) {
+ Integer pop = queue.pop();
+ exeCount++;
+ // 遍历这个pop点的出边
+ List popOut = graph.get(pop);
+ for (int i = 0; i < popOut.size(); i++) {
+ int deleteSideNode = popOut.get(i);
+ inGre[deleteSideNode]--;
+ if (inGre[deleteSideNode] == 0) {
+ queue.add(deleteSideNode);
+ }
+ }
+ }
+ // 如果队列中没有元素了,但还有边,返回false
+ return exeCount == numCourses;
+ }
+ }
+
+
+ class Solution1 {
+ public boolean canFinish(int numCourses, int[][] prerequisites) {
+ // 统计入度个数
+ int[] map = new int[2000];
+ for (int i = 0; i < prerequisites.length; i++) {
+ map[prerequisites[i][0]]++;
+ }
+ // 对所有入度为0的进入队列
+ Deque queue = new ArrayDeque<>();
+ for (int i = 0; i < numCourses; i++) {
+ if (map[i] == 0) {
+ queue.add(i);
+ }
+ }
+ // 出对,并去边
+ int exeCount = 0;
+ while (!queue.isEmpty()) {
+ Integer pop = queue.pop();
+ exeCount++;
+ // 遍历所有的边
+ for (int i = 0; i < prerequisites.length; i++) {
+ if (prerequisites[i][1] == pop) {
+ int deleteSideNode = prerequisites[i][0];
+ map[deleteSideNode]--;
+ if (map[deleteSideNode] == 0) {
+ queue.add(deleteSideNode);
+ }
+ }
+ }
+ }
+ // 如果队列中没有元素了,但还有边,返回false
+ return exeCount == numCourses;
+ }
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode209_2.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode209_2.java
new file mode 100644
index 0000000..b691c5f
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode209_2.java
@@ -0,0 +1,128 @@
+package cn.whaifree.leetCode.Array;
+
+import org.junit.Test;
+
+import java.util.Arrays;
+
+/**
+ * 209. 长度最小的子数组
+ * 给定一个含有 n 个正整数的数组和一个正整数 target 。
+ *
+ * 找出该数组中满足其总和大于等于 target 的长度最小的 连续子数组 [numsl, numsl+1, ..., numsr-1, numsr] ,并返回其长度。如果不存在符合条件的子数组,返回 0 。
+ *
+ * 示例 1:
+ *
+ * 输入:target = 7, nums = [2,3,1,2,4,3]
+ * 输出:2
+ * 解释:子数组 [4,3] 是该条件下的长度最小的子数组。
+ * 示例 2:
+ *
+ * 输入:target = 4, nums = [1,4,4]
+ * 输出:1
+ * 示例 3:
+ *
+ * 输入:target = 11, nums = [1,1,1,1,1,1,1,1]
+ * 输出:0
+ *
+ * 提示:
+ *
+ * 1 <= target <= 109
+ * 1 <= nums.length <= 105
+ * 1 <= nums[i] <= 105
+ *
+ * 进阶:
+ *
+ * 如果你已经实现 O(n) 时间复杂度的解法, 请尝试设计一个 O(n log(n)) 时间复杂度的解法。
+ */
+public class LeetCode209_2 {
+
+ /**
+ * 暴力求解,会超时
+ * @param target
+ * @param nums
+ * @return
+ */
+ public int minSubArrayLen(int target, int[] nums) {
+
+ int minLength = Integer.MAX_VALUE;
+ for (int i = 0; i < nums.length; i++) {
+ int sum = 0;
+ for (int j = i; j < nums.length; j++) {
+ sum += nums[j];
+ if (sum >= target) {
+ minLength = Math.min(minLength, j - i + 1);
+ break;
+ }
+ /**
+ * if (sum >= target && j - i +1 < minLength) {
+ * minLength = j - i + 1;
+ * break;
+ * }
+ */
+ }
+ }
+
+
+ return minLength == Integer.MAX_VALUE ? 0 : minLength;
+ }
+
+ public int minSubArrayLen1(int target, int[] nums) {
+ int left = 0;
+ int right = 0;
+ int sum = 0;
+ int ans = Integer.MAX_VALUE;
+ while (right < nums.length ) {
+ sum += nums[right];
+ // 窗口内,找到最小子串
+ while (sum >= target) {
+ ans = Math.min(ans, right - left + 1);
+ sum -= nums[left++];
+ }
+ right++;
+ }
+ return ans == Integer.MAX_VALUE ? 0 : ans;
+ }
+
+
+
+ class Solution {
+ /**
+ * 前缀和做法
+ * @param target
+ * @param nums
+ * @return
+ */
+ public int minSubArrayLen(int target, int[] nums) {
+ // 求前缀和
+ int length = nums.length;
+ int[] preSum = new int[length + 1];
+ for (int i = 1; i < preSum.length; i++) {
+ preSum[i] = preSum[i - 1] + nums[i - 1];
+ }
+ // 2,3,1,2,4,3
+ // 因为每个元素都是正数,所以preSum是递增的,可以用二分查找
+ int minLengthFillSolution = Integer.MAX_VALUE;
+ for (int i = 1; i < preSum.length; i++) {
+ // 从0开始查找
+ int fill = target + preSum[i - 1];
+ int intervalEnd = Arrays.binarySearch(preSum, fill); // 没找到就会返回负数
+ if (intervalEnd < 0) {
+ intervalEnd = -intervalEnd - 1; // 防止查出来的是负数
+ }
+ // 这个区间如果合理,就可能是正常的区间
+ if (intervalEnd <= length)
+ minLengthFillSolution = Math.min(minLengthFillSolution, intervalEnd - (i - 1));
+ // 注意区分下标 intervalEnd和i-1 前缀和为fill和target
+ }
+ return minLengthFillSolution == Integer.MAX_VALUE ? 0 : minLengthFillSolution;
+ }
+ }
+
+ @Test
+ public void list() {
+ System.out.println(new Solution().minSubArrayLen(7, new int[]{2, 3, 1, 2, 4, 3}));
+
+// System.out.println(minSubArrayLen1(5, new int[]{2,3,6}));
+ }
+
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode21.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode21.java
new file mode 100644
index 0000000..2ecf941
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode21.java
@@ -0,0 +1,79 @@
+package cn.whaifree.leetCode.LinkedList;
+
+import cn.whaifree.leetCode.model.ListNode;
+import org.junit.Test;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/4/4 13:05
+ * @注释
+ */
+public class LeetCode21 {
+
+
+ @Test
+ public void test() {
+ new Solution1().mergeTwoLists(
+ ListNode.listNodeFromArray(new int[]{5}),
+ ListNode.listNodeFromArray(new int[]{1, 2, 4})
+ ).printList();
+ }
+
+ class Solution {
+ public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
+ if (list2 == null) {
+ return list1;
+ }
+ if (list1 == null) {
+ return list2;
+ }
+
+ ListNode pre = new ListNode(-1); // 标记结果头部
+ ListNode preIndex = pre; // 标记需要添加的位置
+ // 合并有序链表
+ ListNode indexA = list1;
+ ListNode indexB = list2;
+ while (indexA != null && indexB != null) {
+ if (indexA.val <= indexB.val) {
+ preIndex.next = indexA;
+ indexA = indexA.next;
+ }else {
+ preIndex.next = indexB;
+ indexB = indexB.next;
+ }
+ preIndex = preIndex.next;
+ }
+
+ if (indexA == null) {
+ preIndex.next = indexB;
+ }else {
+ preIndex.next = indexA;
+ }
+
+ return pre.next;
+ }
+ }
+
+
+ class Solution1 {
+ public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
+
+ if (list1 == null) {
+ return list2;
+ }
+ if (list2 == null) {
+ return list1;
+ }
+
+ if (list1.val < list2.val) {
+ list1.next = mergeTwoLists(list1.next, list2);
+ return list1;
+ }else {
+ list2.next = mergeTwoLists(list1, list2.next);
+ return list2;
+ }
+
+ }
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode210.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode210.java
new file mode 100644
index 0000000..b7b2858
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode210.java
@@ -0,0 +1,98 @@
+package cn.whaifree.leetCode.Graph;
+
+import org.junit.Test;
+
+import java.util.*;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/9/16 0:44
+ * @注释
+ */
+public class LeetCode210 {
+ @Test
+ public void test() {
+ int[][] prerequisites = {{1,0},{2,0},{3,1},{3,2}};
+ int[] res = new Solution().findOrder(4, prerequisites);
+ System.out.println(Arrays.toString(res));
+ }
+
+ public int[] findOrder(int numCourses, int[][] prerequisites) {
+ if (numCourses == 0) return new int[0];
+ int[] inDegrees = new int[numCourses];
+
+
+ // 建立入度表
+ for (int[] p : prerequisites) {
+ inDegrees[p[0]]++; // 记录每个节点的入度
+ }
+ // 入度为0的节点队列
+ Queue queue = new LinkedList<>();
+ for (int i = 0; i < inDegrees.length; i++) {
+ if (inDegrees[i] == 0) queue.offer(i); // 入度为 0 的节点可以进行执行
+ }
+ int count = 0; // 记录可以执行的任务数
+ int[] res = new int[numCourses]; // 完整拓扑排序的执行过程
+
+ // 根据提供的可以执行的任务(入度为 0),删除入度为 0 的节点
+ while (!queue.isEmpty()){
+ int curr = queue.poll(); // 拿到一个可以执行的任务
+ res[count++] = curr; // 这个任务可以执行,作为下一次执行的节点
+ for (int[] p : prerequisites) {
+ if (p[1] == curr){ // {a,b} 表示 a 依赖 b b-->a
+ inDegrees[p[0]]--;
+ if (inDegrees[p[0]] == 0) queue.offer(p[0]);
+ }
+ }
+ }
+ if (count == numCourses) return res;
+ return new int[0];
+ }
+
+
+ class Solution {
+ public int[] findOrder(int numCourses, int[][] prerequisites) {
+ List> graph = new ArrayList<>();
+ for (int i = 0; i < numCourses; i++) {
+ graph.add(new ArrayList<>());
+ }
+ int[] inGre = new int[numCourses];
+
+ Deque deque = new LinkedList<>();
+ for (int[] prerequisite : prerequisites) {
+ int course = prerequisite[0];
+ int pre = prerequisite[1];
+ inGre[course]++;
+ graph.get(pre).add(course);
+ }
+
+ for (int i = 0; i < inGre.length; i++) {
+ if (inGre[i] == 0) {
+ deque.add(i);
+ }
+ }
+
+ int exec = 0;
+ int[] res = new int[numCourses];
+ while (!deque.isEmpty()) {
+ Integer exe = deque.pop();
+ res[exec] = exe;
+ exec++;
+ List in = graph.get(exe);
+ for (int into = 0; into < in.size(); into++) {
+ Integer intoNode = in.get(into);
+ inGre[intoNode]--;
+ if (inGre[intoNode] == 0) {
+ deque.add(intoNode);
+ }
+ }
+ }
+
+ if (numCourses == exec) {
+ return res;
+ }
+ return new int[0];
+ }
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode215.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode215.java
new file mode 100644
index 0000000..74a2a43
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode215.java
@@ -0,0 +1,518 @@
+package cn.whaifree.leetCode.Array;
+
+import org.junit.Test;
+
+import java.io.*;
+import java.util.*;
+import java.util.function.BiConsumer;
+
+import static org.junit.Assert.assertArrayEquals;
+
+public class LeetCode215 {
+
+ public static void main(String[] args) throws FileNotFoundException {
+ String name = "/Users/kyriewhluo/IdeaProjects/tsf/tsf-dispatch/src/main/resources/tsf-dispatch.yml";
+ // 读取每一行
+ HashMap> map = new HashMap<>();
+ try (BufferedReader reader = new BufferedReader(new FileReader(name))) {
+ String line;
+ while ((line = reader.readLine()) != null) {
+ if (line.contains("tsf-resource")) {
+ String[] split = line.split("/");
+ String key = "/" + split[1] + "/" + split[2];
+ List orDefault = map.getOrDefault(key, new ArrayList<>());
+ orDefault.add(line);
+ map.put(key, orDefault);
+ }
+ }
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+
+ map.forEach(new BiConsumer>() {
+ @Override
+ public void accept(String s, List strings) {
+ System.out.println("# key:" + s);
+ System.out.println("# size:" + strings.size());
+ for (String string : strings) {
+ System.out.println(string);
+ }
+
+ for (int i = 0; i < 3; i++) {
+ System.out.println();
+ }
+ }
+ });
+ }
+
+
+ @Test
+ public void sort_EmptyArray_ShouldHandleGracefull3y() {
+ int[] nums = {3,2,1,5,6,4};
+ System.out.println(new Solution5().findKthLargest(nums, 2));
+ System.out.println(Arrays.toString(nums));
+
+ }
+
+ class Solution5 {
+
+ /**
+ * 快速排序,某次base放对位置后,左边刚刚好有k-1个,就找到了
+ * @param nums
+ * @param k
+ * @return
+ */
+ public int findKthLargest(int[] nums, int k) {
+ return sort(nums, 0, nums.length - 1, k);
+ }
+
+ /**
+ * 快速排序思路,对前n-1个进行不断交换,最后把基准替换到交接点
+ * @param nums
+ * @param start
+ * @param end
+ * @param k
+ * @return
+ */
+ public int sort(int[] nums, int start, int end, int k) {
+
+ if (start > end) {
+ return nums[end];
+ }
+
+ int q = new Random().nextInt(end - start + 1) + start;
+
+ swap(nums, q, end);
+
+ int base = nums[end];
+ int left = start;
+ int right = end;
+ while (left < right) {
+
+ //从左往右遍历,当左指针指向的元素小于等于基数时,i++。左指针持续向右移动
+ while (nums[left] >= base && left < right) {
+ left++;
+ }
+ //从右往左遍历,当右指针指向的元素大于等于基数时,j--。右指针持续向左移动
+ while (nums[right] <= base && left < right) {
+ right--;
+ }
+ if (left < right) {
+ //当左右两个指针停下来时,交换两个元素
+ swap(nums, left, right);
+ }
+ }
+ swap(nums, left, end);
+
+ // 从大到小排序,如果左边k-1个,则left就是第k个,左边k-1个比他大
+ if (left == k - 1) {
+ return nums[left];
+ }
+ // 左边的数量太少了,往右边找
+ if (left < k - 1) {
+ return sort(nums, left + 1, end, k);
+ }
+ return sort(nums, start, left - 1, k);
+
+ }
+ public void swap(int[] heap, int start, int end) {
+ int temp = heap[start];
+ heap[start] = heap[end];
+ heap[end] = temp;
+ }
+ }
+
+
+ @Test
+ public void sort_EmptyArray_ShouldHandleGracefully8() {
+
+ // [3,2,3,1,2,4,5,5,6], k = 4
+ int[] nums = {5,4,3,2,1};
+ System.out.println(new Solution4().findKthLargest(nums, 2));
+ }
+
+ class Solution4 {
+ public int findKthLargest(int[] nums, int k) {
+ for (int i = nums.length - 1; i > 0; i--) {
+ shiftUp(nums, i);
+ }
+ System.out.println(Arrays.toString(nums));
+ return nums[nums.length - k];
+ }
+
+ public void shiftUp(int[] heap, int end) {
+ int parent = (end - 1) / 2;
+ while (parent >= 0) {
+ int left = parent * 2 + 1;
+ int right = parent * 2 + 2;
+ int k = parent;
+ if (left <= end &&heap[left] > heap[k]) {
+ k = left;
+ }
+ if (right <= end && heap[right] > heap[k]) {
+ k = right;
+ }
+ swap(heap, parent, k);
+ parent--;
+ }
+ swap(heap, 0, end);
+ }
+
+ public void swap(int[] heap, int start, int end) {
+ int temp = heap[start];
+ heap[start] = heap[end];
+ heap[end] = temp;
+ }
+ }
+
+ @Test
+ public void main()
+ {
+ int i = 0;
+ while (true) {
+ i++;
+ }
+//
+// int[] nums = {3,2,1,5,6,4};
+// int k = 2;
+// Solution solution = new Solution();
+// int i = solution.findKthLargest(nums, k);
+// System.out.println(i);
+ }
+
+// @Test
+// public void test188()
+// {
+// new sol().findKthLargest(new int[]{3, 2, 1, 5, 6, 4}, 3);
+//
+//
+// }
+
+// class sol{
+// /**
+// * 3
+// * / \
+// * / \
+// * 5 -2147483648
+// * /
+// * 2 右边那个有问题,所以不行
+// * @param nums
+// * @param k
+// * @return
+// */
+// public int findKthLargest(int[] nums, int k) {
+// Heap heap = new Heap(k);
+// for (int num : nums) {
+// heap.add(num);
+// }
+// return 1;
+// }
+// }
+
+
+// class Heap{
+//
+//
+// int[] heap = null;
+//
+// public Heap(int k) {
+// this.heap = new int[k + 1];
+// Arrays.fill(this.heap, Integer.MIN_VALUE);
+// }
+//
+// public void add(int num) {
+// heap[heap.length - 1] = num;
+// shiftUp(heap, heap.length - 1);
+// }
+//
+//
+// /**
+// * 固定长度的,让其 shiftUp
+// * @param nums
+// * @param numIndex 将 numsIndex 位置上移
+// */
+// public void shiftUp(int[] nums, int numIndex) {
+// int k = numIndex;
+// while (k > 0) {
+// int parent = (k - 1) / 2;
+//// if (nums[numIndex] < nums[parent]) { // 小顶堆
+// if (nums[k] > nums[parent]) { // 大顶堆
+// // 小顶堆,小的上移
+// swap(nums, parent, k);
+// k = parent;
+// }else {
+// break;
+// }
+// TreeNode.constructTreeByArrayWithInteger(nums);
+// }
+//
+// }
+//
+// public void swap(int[] nums, int start, int end) {
+// int temp = nums[start];
+// nums[start] = nums[end];
+// nums[end] = temp;
+// }
+//
+// }
+
+ class Solution {
+ public int findKthLargest(int[] nums, int k) {
+
+
+ // 小顶堆
+ PriorityQueue priorityQueue = new PriorityQueue<>(new Comparator() {
+ @Override
+ public int compare(Integer o1, Integer o2) {
+ return o1 - o2;
+ }
+ });
+ for (int num : nums) {
+ priorityQueue.offer(num);
+ if (priorityQueue.size() > k) {
+ priorityQueue.poll();
+ }
+ }
+
+ return priorityQueue.poll();
+ }
+ }
+
+ @Test
+ public void test1()
+ {
+
+ int[] nums = {3,2,1,5,6,4};
+ int k = 2;
+ Solution1 solution = new Solution1();
+ int i = solution.findKthLargest(nums, k);
+ System.out.println(i);
+ }
+
+ class Solution1 {
+ public int findKthLargest(int[] nums, int k) {
+ sort(nums, nums.length - 1);
+
+ return nums[k - 1];
+ }
+
+ /**
+ * 堆排序思路:
+ * 1. 依次遍历非叶节点 nonLeaf = (end - 1) / 2; --
+ * 选取左右两边比他大的替换上来,不断替换直到最上面是最大的
+ * 2. 把最大的堆顶移动到最后,确定一个最大值
+ * @param nums
+ * @param end
+ */
+ public void sort(int[] nums, int end) {
+ if (end <= 0) {
+ return;
+ }
+ int heapSize = nums.length;
+ for (int i = heapSize / 2; i >= 0; --i) {
+ int l = i * 2 + 1, r = i * 2 + 2, largest = i;
+ if (l < heapSize && nums[l] > nums[largest]) {
+ largest = l;
+ }
+ if (r < heapSize && nums[r] > nums[largest]) {
+ largest = r;
+ }
+ if (largest != i) {
+ swap(nums, i, largest);
+ sort(nums, heapSize - 1);
+ }
+ }
+ swap(nums, 0, end);
+ }
+
+ public void swap(int[] nums, int start, int end) {
+ int temp = nums[start];
+ nums[start] = nums[end];
+ nums[end] = temp;
+ }
+
+
+ }
+
+
+
+
+ @Test
+ public void sort_EmptyArray_ShouldHandleGracefully() {
+ int[] nums = {};
+ sort(nums);
+ assertArrayEquals(new int[0], nums);
+ }
+
+ @Test
+ public void sort_NaturalNumberArray_ShouldSortInAscendingOrder() {
+ int[] nums = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
+ sort(nums);
+ assertArrayEquals(new int[]{1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9}, nums);
+ }
+
+ @Test
+ public void sort_IntegerArray_ShouldSortInAscendingOrder() {
+ int[] nums = {10, -1, 2, 5, 0, 6, -3, 4};
+ sort(nums);
+ assertArrayEquals(new int[]{-3, -1, 0, 2, 4, 5, 6, 10}, nums);
+ }
+
+ @Test
+ public void sort_SingleElementArray_ShouldRemainUnchanged() {
+ int[] nums = {5};
+ sort(nums);
+ assertArrayEquals(new int[]{5}, nums);
+ }
+
+ @Test
+ public void sort_DuplicateElementsArray_ShouldSortInAscendingOrder() {
+ int[] nums = {4, 2, 2, 8, 3, 3, 1};
+ sort(nums);
+ assertArrayEquals(new int[]{1, 2, 2, 3, 3, 4, 8}, nums);
+ }
+
+ @Test
+ public void sort_NegativeNumberArray_ShouldSortInAscendingOrder() {
+ int[] nums = {-1, -2, -3, -4, -5};
+ System.out.println(Arrays.toString(new Solution3().sortArray(nums)));
+ }
+
+ class Solution3 {
+ public int[] sortArray(int[] nums) {
+ sort(nums, nums.length - 1);
+ return nums;
+ }
+
+ /**
+ * 堆排序思路:
+ * 1. 依次遍历非叶节点 nonLeaf = (end - 1) / 2; --
+ * 选取左右两边比他大的替换上来,不断替换直到最上面是最大的
+ * 2. 把最大的堆顶移动到最后,确定一个最大值
+ * @param nums
+ * @param end
+ */
+ public void sort(int[] nums, int end) {
+ if (end <= 0) {
+ return;
+ }
+
+ int nonLeaf = (end - 1) / 2;
+ while (nonLeaf >= 0) {
+
+
+ int left = 2 * nonLeaf + 1;
+ int right = 2 * nonLeaf + 2;
+
+
+ int maxIn = nonLeaf; // 父子三个节点的最大值
+ if (left <= end && nums[maxIn] < nums[left]) {
+ maxIn = left;
+ }
+ if (right <= end && nums[maxIn] < nums[right]) {
+ maxIn = right;
+ }
+ swap(nums, nonLeaf, maxIn);
+
+
+
+// // noleaf至少有一个子节点
+// if (left <= end &&right <= end) {
+// if (nums[left] < nums[right]) {
+// if (nums[right] > nums[nonLeaf]) {
+// swap(nums, nonLeaf, right);
+// }
+// }else {
+// if (nums[left] > nums[nonLeaf]) {
+// swap(nums, nonLeaf, left);
+// }
+// }
+// } else {
+// // 只有左边一个节点
+// if (nums[left] > nums[nonLeaf]) {
+// swap(nums, nonLeaf, left);
+// }
+// }
+ nonLeaf--;
+ }
+ swap(nums, 0, end );
+ sort(nums, end - 1);
+ }
+
+ public void swap(int[] nums, int start, int end) {
+ int temp = nums[start];
+ nums[start] = nums[end];
+ nums[end] = temp;
+ }
+ }
+
+
+ /**
+ * 所有非叶子节点x(自 n/2 开始,表示下面都是叶子节点)找到子节点中的最大值,如果比x 还大,swap。再排序下一个非叶子节点
+ */
+ public void sort(int[] nums) {
+ sort(nums, nums.length - 1);
+ System.out.println(Arrays.toString(nums));
+ }
+
+ /**
+ * 堆排序思路:
+ * 1. 依次遍历非叶节点 nonLeaf = (end - 1) / 2; --
+ * 选取左右两边比他大的替换上来,不断替换直到最上面是最大的
+ * 2. 把最大的堆顶移动到最后,确定一个最大值
+ * @param nums
+ * @param end
+ */
+ public void sort(int[] nums, int end) {
+ if (end <= 0) {
+ return;
+ }
+
+ int nonLeaf = (end - 1) / 2;
+ while (nonLeaf >= 0) {
+
+
+ int left = 2 * nonLeaf + 1;
+ int right = 2 * nonLeaf + 2;
+
+
+ int maxIn = nonLeaf; // 父子三个节点的最大值
+ if (left <= end && nums[maxIn] < nums[left]) {
+ maxIn = left;
+ }
+ if (right <= end && nums[maxIn] < nums[right]) {
+ maxIn = right;
+ }
+ swap(nums, nonLeaf, maxIn);
+
+
+
+// // noleaf至少有一个子节点
+// if (left <= end &&right <= end) {
+// if (nums[left] < nums[right]) {
+// if (nums[right] > nums[nonLeaf]) {
+// swap(nums, nonLeaf, right);
+// }
+// }else {
+// if (nums[left] > nums[nonLeaf]) {
+// swap(nums, nonLeaf, left);
+// }
+// }
+// } else {
+// // 只有左边一个节点
+// if (nums[left] > nums[nonLeaf]) {
+// swap(nums, nonLeaf, left);
+// }
+// }
+ nonLeaf--;
+ }
+ swap(nums, 0, end );
+ sort(nums, end - 1);
+ }
+
+ public void swap(int[] nums, int start, int end) {
+ int temp = nums[start];
+ nums[start] = nums[end];
+ nums[end] = temp;
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode216.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode216.java
new file mode 100644
index 0000000..2f7b01a
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode216.java
@@ -0,0 +1,81 @@
+package cn.whaifree.leetCode.BackTracking;
+
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/2/2 8:22
+ * @注释
+ */
+public class LeetCode216 {
+
+ @Test
+ public void test() {
+ Solution solution = new Solution();
+ solution.combinationSum3(9, 45).forEach(
+ list -> {
+ System.out.println(list);
+ }
+ );
+ }
+
+ class Solution {
+
+ List> res = new LinkedList<>();
+ List path = new ArrayList<>();
+ int sum = 0;
+ /**
+ * 相加之和为n的k个数的组合
+ * @param k
+ * @param n
+ * @return
+ */
+ public List> combinationSum3(int k, int n) {
+ circle(1, 9, n, k);
+ return res;
+ }
+
+ public void circle(int start, int end, int n, int k) {
+ if (path.size() == k && sum == n) {
+ res.add(new ArrayList<>(path));
+ return;
+ }
+
+ // 1. 如果sum>n了,证明往后加只会更大,因为都是正数,就不再继续了
+ // sum>n
+ //
+ // 2. 如果 9个数要9个数
+ // 已经选择size
+ // 还需选择k-size
+ // 可以选择的数end-start
+ // 可以选择的数<还需选择的数 end-start n) {
+ path.remove(path.size() - 1);
+ sum -= i;
+ return;
+ }
+ circle(i + 1, end, n, k);
+ path.remove(path.size() - 1);
+ sum -= i;
+ }
+ }
+ }
+
+
+
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode222.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode222.java
new file mode 100644
index 0000000..5dd91fb
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode222.java
@@ -0,0 +1,73 @@
+package cn.whaifree.leetCode.Tree;
+
+import cn.whaifree.leetCode.model.TreeNode;
+import org.junit.Test;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/1/22 19:05
+ * @注释
+ */
+public class LeetCode222 {
+
+ @Test
+ public void test() {
+ TreeNode treeNode = TreeNode.constructTree(new Integer[]{1, 2, 3, 4, 5, 6});
+ treeNode.printTree();
+ System.out.println(new Solution1().countNodes(treeNode));
+ }
+
+ class Solution {
+
+
+ public int countNodes(TreeNode root) {
+ return inLevel(root);
+ }
+
+ public int inLevel(TreeNode node) {
+ if (node==null) return 0;
+ int left = inLevel(node.left) + 1;
+ int right = inLevel(node.right) ;
+ return left + right;
+ }
+ }
+
+ class Solution1 {
+
+
+ public int countNodes(TreeNode root) {
+ return inLevel(root);
+ }
+
+ public int inLevel(TreeNode node) {
+ if (node == null) {
+ return 0;
+ }
+ // 计算左边深度
+ int leftDepth = 0;
+ TreeNode leftNode = node.left;
+ while (leftNode != null) {
+ leftNode = leftNode.left;
+ leftDepth++;
+ }
+
+ // 计算右边深度
+ int rightDepth = 0;
+ TreeNode rightNode = node.right;
+ while (rightNode != null) {
+ rightNode = rightNode.right;
+ rightDepth++;
+ }
+
+ // 如果两变深度一样,那么该树是完全二叉树
+ if (leftDepth == rightDepth) {
+ return (2 << leftDepth) - 1;
+ }
+ // 如果两边深度不一样,递归左右子节点+1
+ return inLevel(node.right) + inLevel(node.left) + 1;
+
+ }
+ }
+
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode225.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode225.java
new file mode 100644
index 0000000..68c71c3
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode225.java
@@ -0,0 +1,174 @@
+package cn.whaifree.leetCode.Stack;
+
+import org.junit.Test;
+
+import java.util.Deque;
+import java.util.LinkedList;
+import java.util.Queue;
+
+/**
+ * 225. 用队列实现栈
+ *
+ * 请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。
+ *
+ * 实现 MyStack 类:
+ *
+ * void push(int x) 将元素 x 压入栈顶。
+ * int pop() 移除并返回栈顶元素。
+ * int top() 返回栈顶元素。
+ * boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。
+ *
+ * 注意:
+ *
+ * 你只能使用队列的基本操作 —— 也就是 push to back、peek/pop from front、size 和 is empty 这些操作。
+ * 你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
+ *
+ * 示例:
+ *
+ * 输入:
+ * ["MyStack", "push", "push", "top", "pop", "empty"]
+ * [[], [1], [2], [], [], []]
+ * 输出:
+ * [null, null, null, 2, 2, false]
+ *
+ * 解释:
+ * MyStack myStack = new MyStack();
+ * myStack.push(1);
+ * myStack.push(2);
+ * myStack.top(); // 返回 2
+ * myStack.pop(); // 返回 2
+ * myStack.empty(); // 返回 False
+ *
+ */
+public class LeetCode225 {
+
+ @Test
+ public void test() {
+
+ Deque objects = new LinkedList<>();
+ objects.add(1);
+ objects.add(2);
+ objects.push(3);
+ System.out.println(objects.pop());
+// objects.forEach(x -> System.out.println(x));
+
+
+ MyStack1 myStack = new MyStack1();
+ myStack.push(1);
+ myStack.push(2);
+ myStack.push(3);
+
+ System.out.println(myStack.top());
+ System.out.println(myStack.pop());
+
+ }
+
+
+ /**
+ * 每次入栈就加入其中的非空队列
+ * 出栈就全部移动到另一个队列,并获取最后一个元素
+ *
+ * 队列用add // add 尾巴进
+ * 栈用push // push 头进
+ * pop 头出
+ *
+ * - 栈 push pop
+ * - 队列 add pop
+ */
+ class MyStack {
+
+ Deque queue1;
+ Deque queue2;
+
+ public MyStack() {
+ queue1 = new LinkedList<>();
+ queue2 = new LinkedList<>();
+ }
+
+ public void push(int x) {
+ if (!queue1.isEmpty()) {
+ queue1.add(x);
+ } else {
+ queue2.add(x);
+ }
+ }
+
+ public int pop() {
+ if (!queue1.isEmpty()) {
+ // 将queue全部导入queue2,并获取最后一个元素返回
+ while (queue1.size()!=1) {
+ queue2.add(queue1.pop());
+ }
+ return queue1.pop();
+ } else {
+ // 将queue2全部导入queue1,并获取最后一个元素返回
+ while (queue2.size()!=1) {
+ queue1.add(queue2.pop());
+ }
+ return queue2.pop();
+ }
+ }
+
+ public int top() {
+ if (!queue1.isEmpty()) {
+ // 将queue全部导入queue2,并获取最后一个元素返回
+ while (queue1.size()>1) {
+ queue2.add(queue1.pop());
+ }
+ Integer pop = queue1.pop();
+ queue2.add(pop);
+ return pop;
+ } else {
+ // 将queue2全部导入queue1,并获取最后一个元素返回
+ while (queue2.size()>1) {
+ queue1.add(queue2.pop());
+ }
+
+ Integer pop = queue2.pop();
+ queue1.add(pop);
+ return pop;
+ }
+ }
+
+ public boolean empty() {
+ return queue1.isEmpty() && queue2.isEmpty();
+ }
+ }
+
+
+ /**
+ * push 的时候 把其替换到头部
+ */
+ class MyStack1 {
+
+ Deque queue;
+
+
+ public MyStack1() {
+ queue = new LinkedList<>();
+ }
+
+ public void push(int x) {
+ queue.add(x);
+ int size = queue.size();
+ while (size != 1) {
+ queue.add(queue.pop());
+ size--;
+ }
+ }
+
+ public int pop() {
+ return queue.pop();
+ }
+
+ public int top() {
+ return queue.peek();
+ }
+
+ public boolean empty() {
+ return queue.isEmpty();
+ }
+ }
+
+
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode226.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode226.java
new file mode 100644
index 0000000..f7f8182
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode226.java
@@ -0,0 +1,148 @@
+package cn.whaifree.leetCode.Tree;
+
+import cn.whaifree.leetCode.model.TreeNode;
+import org.junit.Test;
+
+import java.util.Deque;
+import java.util.LinkedList;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/1/19 14:54
+ * @注释
+ */
+public class LeetCode226 {
+
+ @Test
+ public void test() {
+ TreeNode treeNode = TreeNode.constructTree(new Integer[]{1, 2, 3, 4, 5});
+ treeNode.printTree();
+ TreeNode treeNode1 = new Solution2().invertTree(treeNode);
+ treeNode1.printTree();
+ }
+
+ class Solution {
+ public TreeNode invertTree(TreeNode root) {
+ reverse(root);
+ return root;
+ }
+
+ public void reverse(TreeNode root) {
+ if (root == null) {
+ return;
+ }
+ swap(root);
+ invertTree(root.left);
+ invertTree(root.right);
+ }
+
+ public void swap(TreeNode root) {
+ TreeNode tmp = root.left;
+ root.left = root.right;
+ root.right = tmp;
+ }
+ }
+
+ class Solution1 {
+ /**
+ * 层次遍历
+ * @param root
+ * @return
+ */
+ public TreeNode invertTree(TreeNode root) {
+ if (root == null) {
+ return root;
+ }
+ Deque queue = new LinkedList<>();
+ queue.add(root);
+ while (!queue.isEmpty()) {
+ TreeNode pop = queue.pop();
+ swap(pop);
+ if (pop.right != null) {
+ queue.add(pop.right);
+ }
+ if (pop.left != null) {
+ queue.add(pop.left);
+ }
+ }
+ return root;
+ }
+
+
+ public void swap(TreeNode root) {
+ TreeNode tmp = root.left;
+ root.left = root.right;
+ root.right = tmp;
+ }
+ }
+
+ class Solution2 {
+ /**
+ * 先序遍历
+ * @param root
+ * @return
+ */
+ public TreeNode invertTree(TreeNode root) {
+ if (root == null) {
+ return root;
+ }
+ Deque stack = new LinkedList<>();
+ stack.push(root);
+ while (!stack.isEmpty()) {
+ TreeNode pop = stack.pop();
+ swap(pop);
+ if (pop.right != null) {
+ stack.push(pop.right);
+ }
+ if (pop.left != null) {
+ stack.push(pop.left);
+ }
+
+ }
+ return root;
+ }
+
+
+ public void swap(TreeNode root) {
+ TreeNode tmp = root.left;
+ root.left = root.right;
+ root.right = tmp;
+ }
+ }
+
+ class Solution3 {
+ /**
+ * 先序遍历
+ * @param root
+ * @return
+ */
+ public TreeNode invertTree(TreeNode root) {
+ if (root == null) {
+ return root;
+ }
+ Deque stack = new LinkedList<>();
+ stack.push(root);
+ while (!stack.isEmpty()) {
+ TreeNode pop = stack.pop();
+ swap(pop);
+ if (pop.right != null) {
+ stack.push(pop.right);
+ }
+ if (pop.left != null) {
+ stack.push(pop.left);
+ }
+
+ }
+ return root;
+ }
+
+
+ public void swap(TreeNode root) {
+ TreeNode tmp = root.left;
+ root.left = root.right;
+ root.right = tmp;
+ }
+ }
+
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode228.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode228.java
new file mode 100644
index 0000000..5197d06
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode228.java
@@ -0,0 +1,87 @@
+package cn.whaifree.leetCode.Array;
+
+import org.junit.Test;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.AsynchronousFileChannel;
+import java.nio.file.Paths;
+import java.nio.file.StandardOpenOption;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.Future;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/9/22 14:56
+ * @注释
+ */
+public class LeetCode228 {
+
+ public static void main(String[] args) throws IOException {
+ AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(Paths.get("D:\\project\\LeetCode\\README.md"), StandardOpenOption.READ);
+ ByteBuffer buffer = ByteBuffer.allocate(1024);
+ Future result = fileChannel.read(buffer, 0);
+ while (!result.isDone()) {
+ // do something
+ }
+ }
+
+ @Test
+ public void test() {
+ int[] nums = {0, 2};
+ System.out.println(new Solution().summaryRanges(nums));
+ }
+
+ class Solution {
+
+// public List summaryRanges(int[] nums) {
+// List path = new ArrayList<>();
+// List res = new ArrayList<>();
+// for (int i = 1; i < nums.length; i++) {
+// if (nums[i] != nums[i - 1]) {
+// StringBuilder str = new StringBuilder();
+// for (int j = 0; j < path.size()-1; j++) {
+// str.append(path.get(j));
+// str.append("->");
+// }
+// str.append(path.get(path.size() - 1));
+// path.clear();
+// }
+// path.add(String.valueOf(nums[i]));
+// }
+// }
+ public List summaryRanges(int[] nums) {
+
+
+ List res = new ArrayList<>();
+
+ int left = 0;
+ int right = 1;
+ while (right < nums.length) {
+ if (nums[right] != nums[right - 1] + 1) {
+ StringBuilder stringBuilder = new StringBuilder();
+ stringBuilder.append(nums[left]);
+ if (left != right - 1) {
+ stringBuilder.append("->");
+ stringBuilder.append(nums[right - 1]);
+ }
+ res.add(stringBuilder.toString());
+ left = right;
+ }
+ right++;
+ }
+ if (left < nums.length) {
+ StringBuilder stringBuilder = new StringBuilder();
+ stringBuilder.append(nums[left]);
+ if (left != right - 1) {
+ stringBuilder.append("->");
+ stringBuilder.append(nums[right - 1]);
+ }
+ res.add(stringBuilder.toString());
+ }
+ return res;
+ }
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode23.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode23.java
new file mode 100644
index 0000000..910aa3c
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode23.java
@@ -0,0 +1,129 @@
+package cn.whaifree.leetCode.LinkedList;
+
+import cn.whaifree.leetCode.model.ListNode;
+import org.junit.Test;
+
+import java.util.*;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/4/5 12:00
+ * @注释
+ */
+public class LeetCode23 {
+
+ @Test
+ public void test()
+ {
+ new Solution2().mergeKLists(
+ new ListNode[]{
+ ListNode.listNodeFromArray(new int[]{}),
+ ListNode.listNodeFromArray(new int[]{1, 3, 4}),
+ ListNode.listNodeFromArray(new int[]{2, 6})
+ }
+ ).printList();
+ }
+
+ class Solution {
+ public ListNode mergeKLists(ListNode[] lists) {
+ if (lists.length == 0) {
+ return null;
+ }
+ List arrayList = new ArrayList<>(Arrays.asList(lists));
+ return merge(arrayList);
+ }
+
+ public ListNode merge(List lists) {
+ // 删除list中所有null
+ // 使用removeIf()方法删除所有null元素
+ lists.removeIf(Objects::isNull);
+
+ if (lists.size() == 0) {
+ return null;
+ }
+
+ int minIndex = 0;
+ // 找到最小的返回
+ // 这个最小的的下一个就是递归的
+
+ // 找到最小的头节点的位置
+ for (int i = 0; i < lists.size(); i++) {
+ if (lists.get(minIndex).val > lists.get(i).val) {
+ minIndex = i;
+ }
+ }
+ ListNode minNode = lists.get(minIndex);
+ // 去掉本节点
+ lists.set(minIndex, minNode.next);
+ minNode.next = merge(lists);
+ return minNode;
+
+ }
+
+ }
+
+
+ class Solution1 {
+ public ListNode mergeKLists(ListNode[] lists) {
+ for (int i = 1; i < lists.length; i++) {
+ lists[i] = merge(lists[i], lists[i - 1]);
+ }
+ return lists[lists.length - 1];
+ }
+
+ public ListNode merge(ListNode one, ListNode two) {
+ if (one == null) {
+ return two;
+ } else if (two == null){
+ return one;
+ }
+
+ if (one.val < two.val) {
+ one.next = merge(one.next, two);
+ return one;
+ }else {
+ two.next = merge(one, two.next);
+ return two;
+ }
+ }
+
+ }
+
+ class Solution2 {
+ // 时间复杂度:
+ // O(nlogk),其中 k 为 lists的长度,n 为所有链表的节点数之和。
+
+ //著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
+ public ListNode mergeKLists(ListNode[] lists) {
+
+
+ PriorityQueue queue = new PriorityQueue<>(new Comparator() {
+ @Override
+ public int compare(ListNode o1, ListNode o2) {
+ return o1.val - o2.val;
+ }
+ });
+
+ for (ListNode list : lists) {
+ if (list!=null) queue.add(list);
+ }
+
+ ListNode head = new ListNode(-1);
+ ListNode headIndex = head;
+ while (!queue.isEmpty()) {
+ ListNode poll = queue.poll();
+ headIndex.next = poll;
+ headIndex = headIndex.next;
+ if (poll.next != null) {
+ queue.add(poll.next);
+ }
+ }
+
+ return head.next;
+ }
+
+
+ }
+
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode230.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode230.java
new file mode 100644
index 0000000..6e85840
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode230.java
@@ -0,0 +1,48 @@
+package cn.whaifree.leetCode.Tree;
+
+import cn.whaifree.leetCode.model.TreeNode;
+import org.junit.Test;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/10/3 17:00
+ * @注释
+ */
+public class LeetCode230 {
+ @Test
+ public void test() {
+
+
+
+
+
+ TreeNode treeNode = TreeNode.constructTreeByArray(5,3,6,2,4,null,null,1);
+ System.out.println(new Solution().kthSmallest(treeNode, 3));
+ }
+
+ class Solution {
+ int res = 0;
+ public int kthSmallest(TreeNode root, int k) {
+ if (root == null) {
+ return -1;
+ }
+ int left = kthSmallest(root.left, k);
+ if (left != -1) {
+ return left;
+ }
+ res++;
+ if (res == k) {
+ return root.val;
+ }
+ int right = kthSmallest(root.right, k);
+ if (right != -1) {
+ return right;
+ }
+ return -1;
+ }
+
+
+
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode232.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode232.java
new file mode 100644
index 0000000..5564d95
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode232.java
@@ -0,0 +1,122 @@
+package cn.whaifree.leetCode.Stack;
+
+import org.junit.Test;
+
+import java.util.Deque;
+import java.util.LinkedList;
+import java.util.Queue;
+
+/**
+ * 232. 用栈实现队列
+ *
+ * 请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):
+ *
+ * 实现 MyQueue 类:
+ *
+ * void push(int x) 将元素 x 推到队列的末尾
+ * int pop() 从队列的开头移除并返回元素
+ * int peek() 返回队列开头的元素
+ * boolean empty() 如果队列为空,返回 true ;否则,返回 false
+ * 说明:
+ *
+ * 你 只能 使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
+ * 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
+ *
+ * 示例 1:
+ *
+ * 输入:
+ * ["MyQueue", "push", "push", "peek", "pop", "empty"]
+ * [[], [1], [2], [], [], []]
+ * 输出:
+ * [null, null, null, 1, 1, false]
+ *
+ * 解释:
+ * MyQueue myQueue = new MyQueue();
+ * myQueue.push(1); // queue is: [1]
+ * myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
+ * myQueue.peek(); // return 1
+ * myQueue.pop(); // return 1, queue is [2]
+ * myQueue.empty(); // return false
+ *
+ *
+ * 提示:
+ *
+ * 1 <= x <= 9
+ * 最多调用 100 次 push、pop、peek 和 empty
+ * 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)
+ *
+ *
+ * 进阶:
+ *
+ * 你能否实现每个操作均摊时间复杂度为 O(1) 的队列?换句话说,执行 n 个操作的总时间复杂度为 O(n) ,即使其中一个操作可能花费较长时间。
+ */
+public class LeetCode232 {
+
+ @Test
+ public void test() {
+ MyQueue myQueue = new MyQueue();
+ System.out.println(myQueue.empty());
+ myQueue.push(1);
+ myQueue.push(2);
+ myQueue.push(3);
+ myQueue.push(4);
+ System.out.println(myQueue.peek());
+ System.out.println(myQueue.pop());
+ System.out.println(myQueue.empty());
+
+
+ }
+
+
+ /**
+ 你 只能 使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
+ 一个空的队列不会调用 pop 或者 peek 操作
+
+ * // 进队列,全部放到Enter栈
+ * // 一旦要出栈,检查Out栈有没有,如果没有将目前Enter栈全部弹到Out栈,再弹出第一个
+ *
+ */
+ class MyQueue {
+
+ Deque stackEnter;
+ Deque stackOut;
+
+ public MyQueue() {
+ this.stackEnter = new LinkedList<>();
+ this.stackOut = new LinkedList<>();
+ }
+
+ public void push(int x) {
+ stackEnter.push(x);
+ }
+
+ public int pop() {
+ int peek = peek();
+ stackOut.pop();
+ return peek;
+ }
+
+ public int peek() {
+ if (stackOut.isEmpty()) {
+ while (!stackEnter.isEmpty()) {
+ stackOut.push(stackEnter.pop());
+ }
+ }
+ // 不会对空操作
+ return stackOut.peek();
+ }
+
+ public boolean empty() {
+ return stackEnter.isEmpty() && stackOut.isEmpty();
+ }
+ }
+
+/**
+ * Your MyQueue object will be instantiated and called as such:
+ * MyQueue obj = new MyQueue();
+ * obj.push(x);
+ * int param_2 = obj.pop();
+ * int param_3 = obj.peek();
+ * boolean param_4 = obj.empty();
+ */
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode234.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode234.java
new file mode 100644
index 0000000..1284a5a
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode234.java
@@ -0,0 +1,105 @@
+package cn.whaifree.leetCode.LinkedList;
+
+import cn.whaifree.leetCode.model.ListNode;
+import org.junit.Test;
+
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CountDownLatch;
+import java.util.function.BiConsumer;
+
+public class LeetCode234 {
+
+
+ public static void main(String[] args) throws InterruptedException {
+ ConcurrentHashMap map = new ConcurrentHashMap<>();
+ for (int i = 0; i < 10; i++) {
+ map.put("1", 1);
+ }
+
+ CountDownLatch countDownLatch = new CountDownLatch(10);
+
+ new Thread(new Runnable() {
+ @Override
+ public void run() {
+ map.forEach(
+ new BiConsumer() {
+ @Override
+ public void accept(String s, Integer integer) {
+ System.out.println(s + ":" + integer);
+ }
+ }
+ );
+ }
+ }).start();
+
+ new Thread(new Runnable() {
+ @Override
+ public void run() {
+ for (int i = 0; i < 10; i++) {
+ map.put("i" + i, i);
+ countDownLatch.countDown();
+ }
+ }
+ }).start();
+
+ countDownLatch.await();
+ System.out.println(map.size());
+ }
+
+
+ @Test
+ public void test()
+ {
+ ListNode listNode = ListNode.listNodeFromArray(new int[]{1, 3, 3,2, 1});
+ System.out.println(new Solution().isPalindrome(listNode));
+ }
+ /**
+ * 1. 截取一半
+ * - 逆转、对比
+ * 2. 输出到list中,再回问判断
+ * 3.
+ */
+
+ class Solution {
+ public boolean isPalindrome(ListNode head) {
+ int len = 0;
+ ListNode index = head;
+ while (index != null) {
+ len++;
+ index = index.next;
+ }
+
+ index = head;
+ for (int i = 0; i < len / 2; i++) {
+ index = index.next;
+ }
+
+ ListNode newHead = reverseList(index);
+
+
+ ListNode A = head;
+ ListNode B = newHead;
+ while (B != null) {
+ if (A.val != B.val) {
+ return false;
+ }
+ A = A.next;
+ B = B.next;
+ }
+ return true;
+ }
+
+ private ListNode reverseList(ListNode head) {
+ ListNode prev = null;
+ ListNode curr = head;
+ while (curr != null) {
+ ListNode nextTemp = curr.next;
+ curr.next = prev;
+ prev = curr;
+ curr = nextTemp;
+ }
+ return prev;
+ }
+
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode235.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode235.java
new file mode 100644
index 0000000..61a1d0d
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode235.java
@@ -0,0 +1,66 @@
+package cn.whaifree.leetCode.Tree;
+
+import cn.whaifree.leetCode.model.TreeNode;
+import org.junit.Test;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/1/30 21:10
+ * @注释
+ */
+public class LeetCode235 {
+
+ @Test
+ public void test() {
+ TreeNode treeNode = TreeNode.constructTreeByArray(6,2,8,0,4,7,9,null,null,3,5);
+
+ new Solution().lowestCommonAncestor(treeNode, treeNode.left.right, treeNode.left).printTree();
+ }
+
+ class Solution {
+ /**
+ * 1. p,q在两边,直接返回
+ * 2. pq在左边,向左递归
+ * 3. pq在右边,向右递归
+ * @param root
+ * @param p
+ * @param q
+ * @return
+ */
+ public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
+// if (root == null) {
+// return null;
+// }
+// if (root == p || root == q) {
+// // 找到元素
+// return root;
+// }
+
+// if (p.val < root.val && q.val > root.val) {
+// return root;
+// }
+// if (p.val > root.val && q.val < root.val) {
+// return root;
+// }
+
+ // 只有这个有用
+ if (p.val > root.val && q.val > root.val) {
+ // 向右递归
+ return lowestCommonAncestor(root.right, p, q);
+ } else if (p.val < root.val && q.val < root.val) {
+ // 向左递归
+ return lowestCommonAncestor(root.left, p, q);
+ }
+
+
+ return root;
+ }
+
+ public TreeNode lowestCommonAncestor1(TreeNode root, TreeNode p, TreeNode q) {
+ if (root.val > p.val && root.val > q.val) return lowestCommonAncestor1(root.left, p, q);
+ if (root.val < p.val && root.val < q.val) return lowestCommonAncestor1(root.right, p, q);
+ return root;
+ }
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode236.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode236.java
new file mode 100644
index 0000000..53ddd52
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode236.java
@@ -0,0 +1,100 @@
+package cn.whaifree.leetCode.Tree;
+
+import cn.whaifree.leetCode.model.TreeNode;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/1/30 17:35
+ * @注释
+ */
+public class LeetCode236 {
+ @Test
+ public void test() {
+ TreeNode treeNode = TreeNode.constructTreeByArray(1, 2, 3, 4, 5, 6);
+
+ new Solution1().lowestCommonAncestor(treeNode, treeNode.left.right, treeNode.left).printTree();
+ }
+
+
+ class Solution {
+ public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
+
+ // 先找到两个树
+ // 用两个List记录路径
+ // 挨个对比,找到最后一个公共的点
+ ArrayList e1 = new ArrayList<>();
+ findNode(root, p, e1);
+ ArrayList e2 = new ArrayList<>();
+ findNode(root, q, e2);
+
+
+
+
+ int minSize = Math.min(e1.size(), e2.size());
+ int index = minSize - 1;
+ while (index >= 0) {
+ if (e1.get(index) == e2.get(index)) {
+ return e1.get(index);
+ }
+ index--;
+ }
+
+ return null;
+ }
+
+ public boolean findNode(TreeNode root, TreeNode wantFindNode, List path) {
+
+ if (root==null) return false;
+ path.add(root);
+ if (root == wantFindNode) {
+ return true;
+ }
+
+ boolean left = findNode(root.left, wantFindNode, path);
+ if (left == true) {
+ return true;
+ }
+
+ boolean right = findNode(root.right, wantFindNode, path);
+ if (right == true) {
+ return true;
+ }
+
+ path.remove(path.size() - 1);
+
+ return left || right;
+ }
+ }
+
+ class Solution1 {
+ public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
+ if (root==null) return null;
+ // 左右找到两个树
+ // 如果找到树了,返回该节点
+ if (root.val == p.val || root.val == q.val) {
+ return root;
+ }
+
+ TreeNode left = lowestCommonAncestor(root.left, p, q);
+ TreeNode right = lowestCommonAncestor(root.right, p, q);
+
+ if(left == null && right == null) { // 若未找到节点 p 或 q
+ return null;
+ }else if(left == null && right != null) { // 若找到一个节点
+ return right;
+ }else if(left != null && right == null) { // 若找到一个节点
+ return left;
+ }else { // 若找到两个节点
+ return root;
+ }
+ }
+
+
+ }
+
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode238.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode238.java
new file mode 100644
index 0000000..1314c6e
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode238.java
@@ -0,0 +1,77 @@
+package cn.whaifree.leetCode.Array;
+
+import org.junit.Test;
+
+import java.util.Arrays;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/9/17 13:58
+ * @注释
+ */
+public class LeetCode238 {
+ @Test
+ public void test() {
+ int[] nums = {1, 2, 3, 4};
+ int[] ints = productExceptSelf(nums);
+ for (int anInt : ints) {
+ System.out.println(anInt);
+ }
+ }
+
+ class Solution {
+ /**
+ * [-1,1,0,-3,3]
+ * -1 -1 0 0 0
+ * 0 0 0 -9 3
+ *
+ * @param nums
+ * @return
+ */
+ public int[] productExceptSelf(int[] nums) {
+ int[] preMul = new int[nums.length];
+ preMul[0] = nums[0];
+ for (int i = 1; i < nums.length; i++) {
+ preMul[i] = preMul[i - 1] * nums[i];
+ }
+
+ int[] afterMul = new int[nums.length];
+ afterMul[nums.length - 1] = nums[nums.length - 1];
+ for (int i = nums.length - 2; i >= 0; i--) {
+ afterMul[i] = afterMul[i + 1] * nums[i];
+ }
+
+ for (int i = 0; i < nums.length; i++) {
+ int pre = (i > 0) ? preMul[i - 1] : 1;
+ int after = i < nums.length - 1 ? afterMul[i + 1] : 1;
+ nums[i] = pre * after;
+ }
+
+ return nums;
+ }
+ }
+
+ //维护两个变量,beforeSum表示前缀和,afterSum表示后缀和
+ // 两个指针不断缩小会相交
+ public int[] productExceptSelf(int[] nums) {
+ int n = nums.length;
+ int[] ans = new int[n];
+ Arrays.fill(ans, 1);
+ int beforeSum = 1;
+ int afterSum = 1;
+
+ int left = 0;
+ int right = nums.length - 1;
+ while (left < nums.length) {
+ ans[left] *= beforeSum;
+ ans[right] *= afterSum;
+ beforeSum *= nums[left];
+ afterSum *= nums[right];
+ left++;
+ right--;
+ }
+
+ return ans;
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode239.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode239.java
new file mode 100644
index 0000000..5e4efc9
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode239.java
@@ -0,0 +1,94 @@
+package cn.whaifree.leetCode.Stack;
+
+import org.junit.Test;
+
+import java.util.Deque;
+import java.util.LinkedList;
+
+/**
+ * @Author whai文海
+ * @Date 2024/1/9 14:49
+ * @注释
+ *
+ * 239. 滑动窗口最大值
+
+ * 给你一个整数数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。
+ *
+ * 返回 滑动窗口中的最大值 。
+ *
+ *
+ *
+ * 示例 1:
+ *
+ * 输入:nums = [1,3,-1,-3,5,3,6,7], k = 3
+ * 输出:[3,3,5,5,6,7]
+ * 解释:
+ * 滑动窗口的位置 最大值
+ * --------------- -----
+ * [1 3 -1] -3 5 3 6 7 3
+ * 1 [3 -1 -3] 5 3 6 7 3
+ * 1 3 [-1 -3 5] 3 6 7 5
+ * 1 3 -1 [-3 5 3] 6 7 5
+ * 1 3 -1 -3 [5 3 6] 7 6
+ * 1 3 -1 -3 5 [3 6 7] 7
+ * 示例 2:
+ *
+ * 输入:nums = [1], k = 1
+ * 输出:[1]
+ */
+public class LeetCode239 {
+
+ @Test
+ public void test() {
+ int[] nums = new int[]{1, 3, -1, -3, 5, 3, 6, 7};
+ int[] res = new Solution().maxSlidingWindow(nums, 3);
+ for (int re : res) {
+ System.out.println(re);
+ }
+ }
+
+ class Solution {
+ /**
+ * 维持一个单调队列,队列里存放的是对应值得下标。
+ * 如果 此次队列头部指针为i,即num[i]为目前窗口的最大值。
+ * - 如果这个最大值的下标不在下一个窗口的范围内,那么需要将这个值删除
+ * @param nums
+ * @param k
+ * @return
+ */
+ public int[] maxSlidingWindow(int[] nums, int k) {
+ // 单调队列,维持一个单调递减的队列
+ Deque queue = new LinkedList<>();
+
+ // n个元素,k个窗口,一共为n-k个输出
+ int[] res = new int[nums.length - k + 1];
+
+
+ for (int i = 0; i < nums.length; i++) {
+
+ // 判断当前队首的值是否在本次的窗口内,i-k为窗口左边
+ if (!queue.isEmpty()&&queue.peek() <= i - k) {
+ queue.pop();
+ }
+
+ // 保证单调递减
+ while (!queue.isEmpty() && nums[i] >= nums[queue.peekLast()]) {
+ queue.removeLast();
+ }
+ queue.addLast(i);
+
+
+ // 从第k个开始才有结果
+ if(i+1 >= k){
+ res[i+1-k] = nums[queue.peek()];
+ }
+
+ // 从第k个开始,先把值存进去,在下次循环判断是否这个值存在于这个新的区间中
+ }
+
+
+ return res;
+
+ }
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode24.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode24.java
new file mode 100644
index 0000000..da8f13f
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode24.java
@@ -0,0 +1,113 @@
+package cn.whaifree.leetCode.LinkedList;
+
+import org.junit.Test;
+import cn.whaifree.leetCode.model.ListNode;
+
+/**
+ * 两两交换链表中的节点
+ * 中等
+ * 相关标签
+ * 相关企业
+ * 给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。
+ *
+ * 示例 1:
+ * 输入:head = [1,2,3,4]
+ * 输出:[2,1,4,3]
+ *
+ * 示例 2:
+ * 输入:head = []
+ * 输出:[]
+ *
+ * 示例 3:
+ * 输入:head = [1]
+ * 输出:[1]
+ *
+ */
+public class LeetCode24 {
+
+ /**
+ * Definition for singly-linked list.
+ * public class ListNode {
+ * int val;
+ * ListNode next;
+ * ListNode() {}
+ * ListNode(int val) { this.val = val; }
+ * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
+ * }
+ * pre index tmp
+ * 0 -> 1 -> 2 -> 3 -> 4 -> 5
+ *
+ * pre index tmp
+ * 0 1 -> 2 -> 3 -> 4 -> 5
+ * ----------^
+ *
+ * -----------
+ * pre index tmp |
+ * 0 1 -> 2 -> 3 -> 4 -> 5
+ * ----------^
+ *
+ * -----------
+ * pre index tmp |
+ * 0 1 <- 2 -> 3 -> 4 -> 5
+ * ----------^
+ *
+ * pre tmp index
+ * 0-> 2 -> 1 -> 3 -> 4 -> 5
+ *
+ *
+ * pre tmp index
+ * 0-> 2 -> 1 -> 3 -> 4 -> 5
+ */
+ public ListNode swapPairs(ListNode head) {
+ if (head == null) {
+ return head;
+ }
+ ListNode pre = new ListNode(0, head);
+ ListNode tmp = head.next;
+ ListNode index = head;
+ head = pre;
+ while (tmp != null) {
+ pre.next = tmp;
+ index.next = tmp.next;
+ tmp.next = index;
+ pre = index;
+ index = pre.next;
+ tmp = index == null ? null : index.next;
+ }
+ return head.next;
+ }
+
+ public ListNode swapPairsD(ListNode head) {
+ if (head == null) {
+ return head;
+ }
+ return swap(head, head.next);
+ }
+
+ /**
+ * 递归的关键:
+ * 1. 结束条件
+ * 2. 递归条件(只考虑当前这一点点的情况,剩下的交给下次递归)
+ * @param pre
+ * @param after
+ * @return
+ */
+ public ListNode swap(ListNode pre, ListNode after) {
+ if (pre == null || after == null) {
+ return pre;
+ }
+ // 递归后面的指针
+ pre.next = swap(after.next, after.next == null ? null : after.next.next);
+ after.next = pre;
+
+ return after;
+ }
+
+
+ @Test
+ public void test() {
+ ListNode listNode = swapPairsD(ListNode.listNodeFromArray(new int[]{1,2,3,4,5}));
+ ListNode.printList(listNode);
+
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode2487.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode2487.java
new file mode 100644
index 0000000..bab2d86
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode2487.java
@@ -0,0 +1,98 @@
+package cn.whaifree.leetCode.LinkedList;
+
+import cn.whaifree.leetCode.model.ListNode;
+import org.junit.Test;
+
+public class LeetCode2487 {
+ @Test
+ public void test() {
+ new Solution1().removeNodes(ListNode.listNodeFromArray(new int[]{5, 2, 13, 3, 8})).printList();
+ }
+
+
+ /**
+ * 找到最大的节点,找到下一个最大的节点,此区间全部删掉。
+ * 每次删除后MaxNode为null
+ *
+ * 即最后变成了递减的链表
+ *
+ */
+ class Solution {
+ /**
+ * 递归
+ * @param head
+ * @return
+ */
+// public ListNode removeNodes(ListNode head) {
+// ListNode dummy = new ListNode(0, head);
+// ListNode pre = dummy;
+// ListNode index = head;
+// ListNode maxNode = dummy;
+// while (index != null) {
+// if (index.val > maxNode.val) {
+// maxNode = index;
+// // 删除pre到maxNode的前一个节点
+// pre.next = maxNode;
+// maxNode.next = removeNodes(maxNode.next);
+// }
+// index = index.next;
+// }
+//
+// return dummy.next;
+// }
+ public ListNode removeNodes(ListNode head) {
+ return remove(new ListNode(0, head));
+ }
+
+ public ListNode remove(ListNode head) {
+ if (head == null) {
+ return null;
+ }
+
+ head.next = remove(head.next);
+ if (head.next != null && head.val < head.next.val) {
+ return head.next;
+ } else {
+ return head;
+ }
+ }
+
+ }
+
+ class Solution1 {
+
+
+ public ListNode removeNodes(ListNode head) {
+
+ ListNode newHead = reverse(head);
+ // 变成从右往左边移除比newHead小的
+ ListNode index = newHead;
+ ListNode tmpMaxNode = newHead;
+ while (index.next != null) {
+ if (index.next.val < tmpMaxNode.val) {
+ index.next = index.next.next;
+ } else {
+ index = index.next;
+ tmpMaxNode = index;
+ }
+ }
+ // 最后那个数是永远不会删除的
+ return reverse(newHead);
+ }
+
+ public ListNode reverse(ListNode head) {
+ // 反转链表
+ ListNode index = head;
+ ListNode pre = null;
+
+ while (index != null) {
+ ListNode tmp = index.next;
+ index.next = pre;
+ pre = index;
+ index = tmp;
+ }
+ // {5, 2, 13, 3, 8}
+ return pre;
+ }
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode25.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode25.java
new file mode 100644
index 0000000..736a315
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode25.java
@@ -0,0 +1,62 @@
+package cn.whaifree.leetCode.LinkedList;
+
+import cn.whaifree.leetCode.model.ListNode;
+import org.junit.Test;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/9/23 13:43
+ * @注释
+ */
+public class LeetCode25 {
+
+ @Test
+ public void test() {
+ ListNode head = ListNode.listNodeFromArray(new int[]{1, 2, 3, 6, 7, 8, 9, 20});
+// new Solution().reverse(null, head, 3).printList();
+ new Solution().reverseKGroup(head, 2).printList();
+ }
+
+ class Solution {
+ public ListNode reverseKGroup(ListNode head, int k) {
+ ListNode pre = new ListNode(-1, head);
+ return reverseRange(pre.next, k);
+ }
+
+
+ /**
+ * pre往后k个元素翻转
+ * @param pre
+ * @param k
+ * @return
+ */
+ public ListNode reverseRange(ListNode pre, int k) {
+ ListNode index = pre;
+ ListNode nextNode = pre;
+ int i = 0;
+ while (i < k && nextNode != null) {
+ nextNode = nextNode.next;
+ i++;
+ }
+ if (i < k) {
+ return pre;
+ }
+ ListNode reverseAfterHead = reverse(null, index, k);
+ index.next = reverseRange(nextNode, k);
+ return reverseAfterHead;
+ }
+
+ public ListNode reverse(ListNode pre, ListNode after,int k) {
+ if (k <= 0) {
+ return pre;
+ }
+ if (after == null) {
+ return pre;
+ }
+ ListNode next = after.next;
+ after.next = pre;
+ return reverse(after, next, k - 1);
+ }
+ }
+}
diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode257.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode257.java
new file mode 100644
index 0000000..e3db3a3
--- /dev/null
+++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode257.java
@@ -0,0 +1,173 @@
+package cn.whaifree.leetCode.Tree;
+
+import cn.whaifree.leetCode.model.Node;
+import cn.whaifree.leetCode.model.TreeNode;
+import org.junit.Test;
+
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Deque;
+import java.util.List;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/1/22 21:54
+ * @注释
+ */
+public class LeetCode257 {
+
+ @Test
+ public void test() {
+
+ TreeNode root = TreeNode.constructTree(new Integer[]{1, 2, 3, 5,6,7,8,9,0,6});
+ root.printTree();
+
+ System.out.println(new Solution3().binaryTreePaths(root));
+ }
+ class Solution {
+
+ List res = new ArrayList();
+
+ public List binaryTreePaths(TreeNode root) {
+ if (root == null) {
+ return res;
+ }
+ deal(root, "");
+ return res;
+ }
+
+ public void deal(TreeNode root, String s) {
+
+ if (root == null) return;
+ if (root.left == null && root.right == null) {
+ res.add(new StringBuilder(s).append(root.val).toString());
+ }
+ String tmp = new StringBuilder(s).append(root.val).append("->").toString();
+ deal(root.left, tmp);
+ deal(root.right, tmp);
+ }
+
+ }
+
+
+ class Solution1 {
+
+ List res = new ArrayList();
+
+ public List binaryTreePaths(TreeNode root) {
+ if (root == null) {
+ return res;
+ }
+ deal(root, "");
+ return res;
+ }
+
+ public void deal(TreeNode treeNode, String s) {
+
+ if (treeNode==null) return;
+ //根节点
+ if (treeNode.right == null && treeNode.left == null) {
+ res.add(s + treeNode.val);
+// res.add(new StringBuilder().append(s).append(treeNode.val).toString());
+ }
+
+// String tmp = new StringBuilder().append(s).append("->").append(treeNode.val).toString();
+ String tmp = s + treeNode.val + "->";
+ deal(treeNode.left, tmp);
+ deal(treeNode.right, tmp);
+ }
+
+
+
+ }
+
+ class Solution2 {
+
+ List res = new ArrayList();
+ List route = new ArrayList();
+
+
+ public List binaryTreePaths(TreeNode root) {
+ if (root == null) {
+ return res;
+ }
+ deal(root);
+ return res;
+ }
+
+ public void deal(TreeNode treeNode) {
+ if (treeNode == null) {
+ return;
+ }
+ route.add(treeNode.val);
+ if (treeNode.left == null && treeNode.right == null) {
+ // 根节点
+ // 可以知道结果了
+ StringBuilder stringBuilder = new StringBuilder();
+ int size = route.size();
+ for (int i = 0; i < size - 1; i++) {
+ stringBuilder.append(route.get(i)).append("->");
+ }
+ stringBuilder.append(route.get(size - 1));
+ res.add(stringBuilder.toString());
+ return;
+ }
+ if (treeNode.right != null) {
+ deal(treeNode.right);
+ // 回溯
+ route.remove(route.size() - 1);
+ }
+ if (treeNode.left != null) {
+ deal(treeNode.left);
+ route.remove(route.size() - 1);
+ }
+
+ }
+
+
+
+ }
+
+ class Solution3 {
+
+
+ public List binaryTreePaths(TreeNode root) {
+ List res = new ArrayList();
+
+ if (root == null) {
+ return res;
+ }
+
+ Deque