diff --git a/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode131.java b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode131.java new file mode 100644 index 0000000..0090241 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode131.java @@ -0,0 +1,114 @@ +package cn.whaifree.redo.redo_all_240721; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/1 21:45 + * @注释 + */ +public class LeetCode131 { + + public static void main(String[] args) { + new Solution1().partition("aab").forEach(System.out::println); + } + + static class Solution { + + + /** + * 分割回文串 + * aab + *
+ * boolean[][] dp + *
+ * dp[i][j]表示i~j是不是回文 + *
+ * if dp[i+1][j-1]==true && s[i] == s[j] + * dp[i][j] == true + *
+ * i从大到小 + * j从小到大 + *
+ *
+ * 初始化 + * i=j true + *
+ * a a b + *
+ * 0 1 2 3
+ * 0 1
+ * 1 1 1 0
+ * 2 1 0
+ * 3 1
+ *
+ * @param s
+ * @return
+ */
+ public List> partition(String s) {
+
+ List
> res = new ArrayList<>();
+ int length = s.length();
+ boolean[][] dp = new boolean[length + 1][length + 1];
+
+ Set
> res = new ArrayList<>();
+ List
> partition(String s) {
+ back(s, 0);
+ return res;
+ }
+
+ public void back(String s, int start) {
+ if (start >= s.length()) {
+ res.add(new ArrayList<>(path));
+ return;
+ }
+ for (int i = start; i < s.length(); i++) {
+ if (isHuiWen(s, start, i)) {
+ path.add(s.substring(start, i + 1));
+ back(s, i + 1);
+ path.remove(path.size() - 1);
+ }
+ }
+ }
+
+ public boolean isHuiWen(String s, int start, int end) {
+ while (start < end) {
+ if (s.charAt(start) != s.charAt(end)) {
+ return false;
+ }
+ start++;
+ end--;
+ }
+ return true;
+ }
+ }
+}
diff --git a/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode40.java b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode40.java
new file mode 100644
index 0000000..923e23d
--- /dev/null
+++ b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode40.java
@@ -0,0 +1,56 @@
+package cn.whaifree.redo.redo_all_240721;
+
+import java.util.*;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/7/29 21:53
+ * @注释
+ */
+
+public class LeetCode40 {
+ public static void main(String[] args) {
+ new LeetCode40().combinationSum2(new int[]{14,6,25,9,30,20,33,34,28,30,16,12,31,9,9,12,34,16,25,32,8,7,30,12,33,20,21,29,24,17,27,34,11,17,30,6,32,21,27,17,16,8,24,12,12,28,11,33,10,32,22,13,34,18,12}, 27).forEach(
+ list -> {
+ list.forEach(
+ integer -> System.out.print(integer + " ")
+ );
+ System.out.println();
+ }
+ );
+ }
+
+ List
> res = new ArrayList<>();
+ List
> combinationSum2(int[] candidates, int target) {
+ Arrays.sort(candidates);
+ used = new boolean[candidates.length];
+ back(candidates, target, 0);
+ return res;
+ }
+
+ public void back(int[] candidates, int target, int start) {
+ if (nowSum == target) {
+ res.add(new ArrayList<>(path));
+ return;
+ }
+ if (nowSum > target) {
+ return;
+ }
+ for (int i = start; i < candidates.length; i++) {
+ if (i > 0 && candidates[i] == candidates[i - 1] && used[i - 1] == false) {
+ continue;
+ }
+ path.add(candidates[i]);
+ nowSum += candidates[i];
+ used[i] = true; // 这个被使用了
+ back(candidates, target, i + 1);
+ used[i] = false;
+ path.remove(path.size() - 1);
+ nowSum -= candidates[i];
+ }
+ }
+}
diff --git a/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode450.java b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode450.java
new file mode 100644
index 0000000..c0d14ea
--- /dev/null
+++ b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode450.java
@@ -0,0 +1,41 @@
+package cn.whaifree.redo.redo_all_240721;
+
+import cn.whaifree.leetCode.model.TreeNode;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/7/28 22:36
+ * @注释
+ */
+public class LeetCode450 {
+
+ public static void main(String[] args) {
+// deleteNode(TreeNode.constructTreeByArray(5, 3, 6, 2, 4, null, 7), 3).printTree();
+ deleteNode(TreeNode.constructTreeByArray(50,30,70,null,40,60,80), 50).printTree();
+ }
+ public static TreeNode deleteNode(TreeNode root, int key) {
+ if (root == null) {
+ return root;
+ }
+ if (root.val == key) {
+ TreeNode tmp = root.right;
+ if (tmp == null) {
+ return root.left;
+ }
+ TreeNode tmpLeft = root.left;
+ TreeNode index = tmp;
+ while (index.left != null) {
+ index = index.left;
+ }
+ index.left = tmpLeft;
+ root = tmp;
+ } else if (root.val > key) {
+ root.left = deleteNode(root.left, key);
+ } else {
+ root.right = deleteNode(root.right, key);
+ }
+ return root;
+ }
+
+}
diff --git a/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode501.java b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode501.java
new file mode 100644
index 0000000..6acd8fd
--- /dev/null
+++ b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode501.java
@@ -0,0 +1,55 @@
+package cn.whaifree.redo.redo_all_240721;
+
+import cn.whaifree.leetCode.model.TreeNode;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/7/28 22:52
+ * @注释
+ */
+public class LeetCode501 {
+
+ public static void main(String[] args) {
+ TreeNode treeNode = TreeNode.constructTreeByArray(1,1,2,null,null,2);
+ int[] mode = new LeetCode501().findMode(treeNode);
+ for (int j : mode) {
+ System.out.println(j);
+ }
+ }
+
+ List
> res = new ArrayList<>();
+ List
> subsetsWithDup(int[] nums) {
+ used = new boolean[nums.length];
+ Arrays.sort(nums);
+ back(nums, 0);
+ return res;
+ }
+
+ public void back(int[] nums, int start) {
+ if (start > nums.length) {
+ return;
+ }
+ res.add(new ArrayList<>(path));
+
+ for (int i = start; i < nums.length; i++) {
+ if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]) {
+ continue;
+ }
+ path.add(nums[i]);
+ used[i] = true;
+ back(nums, i + 1);
+ used[i] = false;
+ path.remove(path.size() - 1);
+ }
+ }
+}