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 o = new HashSet<>(); + + for (int i = length; i > 0; i--) { + for (int j = i; j <= length; j++) { + if (i == j) { + o.add(s.substring(i - 1, j)); + dp[i][j] = true; + continue; + } + if ((i == j - 1 || dp[i + 1][j - 1]) && s.charAt(i - 1) == s.charAt(j - 1)) { + o.add(s.substring(i - 1, j)); + dp[i][j] = true; + } + } + } + o.forEach(o1 -> System.out.println(o1)); +// res.add(o); + return res; + } + } + + static class Solution1 { + + + List> res = new ArrayList<>(); + List path = new ArrayList<>(); + public 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 path = new ArrayList<>(); + int nowSum = 0; + boolean[] used = null; + public 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<>(); + int max = 0; + int tmpV = Integer.MAX_VALUE; + int tmpCount = 1; + public int[] findMode(TreeNode root) { + find(root); + return res.stream().mapToInt(Integer::intValue).toArray(); + } + + public void find(TreeNode root) { + if (root == null) { + return; + } + find(root.left); + if (tmpV == root.val) { + tmpCount++; + }else { + tmpV = root.val; + tmpCount = 1; + } + if (tmpCount > max) { + max = tmpCount; + res.clear(); + res.add(root.val); + } else if (tmpCount == max) { + res.add(root.val); + } + find(root.right); + + } + +} diff --git a/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode701.java b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode701.java new file mode 100644 index 0000000..ae686a5 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode701.java @@ -0,0 +1,33 @@ +package cn.whaifree.redo.redo_all_240721; + +import cn.whaifree.leetCode.model.TreeNode; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/7/29 21:43 + * @注释 + */ +public class LeetCode701 { + + + public static void main(String[] args) { + + new LeetCode701().insertIntoBST(TreeNode.constructTreeByArray(4, 2, 7, 1, 3), 5).printTree(); + new LeetCode701().insertIntoBST(TreeNode.constructTreeByArray(40,20,60,10,30,50,70), 25).printTree(); + + + } + public TreeNode insertIntoBST(TreeNode root, int val) { + if (root == null) { + return new TreeNode(val); + } + if (root.val > val) { + root.left = insertIntoBST(root.left, val); + } else if (root.val < val) { + root.right = insertIntoBST(root.right, val); + } + return root; + } + +} diff --git a/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode90.java b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode90.java new file mode 100644 index 0000000..f451363 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode90.java @@ -0,0 +1,49 @@ +package cn.whaifree.redo.redo_all_240721; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/1 21:33 + * @注释 + */ +public class LeetCode90 { + + public static void main(String[] args) { + int[] nums = {1, 2, 2}; + LeetCode90 leetCode90 = new LeetCode90(); + System.out.println(leetCode90.subsetsWithDup(nums)); + } + + List> res = new ArrayList<>(); + List path = new ArrayList<>(); + boolean[] used = null; + + public 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); + } + } +}