From 28a454f3ae1eee7a876411e871999304c9942c45 Mon Sep 17 00:00:00 2001 From: whaifree <49432110+whaibetter@users.noreply.github.com> Date: Fri, 2 Aug 2024 23:47:30 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=A5commit=E5=8C=85=E5=90=AB=E4=BA=86?= =?UTF-8?q?=E5=AF=B9=E5=9B=9B=E4=B8=AA=E4=B8=8D=E5=90=8C=E7=9A=84LeetCode?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E7=9A=84=E8=A7=A3=E5=86=B3=E6=96=B9=E6=A1=88?= =?UTF-8?q?=E3=80=82=E6=AF=8F=E4=B8=AA=E9=97=AE=E9=A2=98=E9=83=BD=E6=98=AF?= =?UTF-8?q?=E9=80=9A=E8=BF=87=E4=B8=80=E4=B8=AA=E5=8D=95=E7=8B=AC=E7=9A=84?= =?UTF-8?q?Java=E7=B1=BB=E5=AE=9E=E7=8E=B0=E7=9A=84=EF=BC=8C=E8=BF=99?= =?UTF-8?q?=E4=BA=9B=E7=B1=BB=E8=A2=AB=E6=B7=BB=E5=8A=A0=E5=88=B0`cn.whaif?= =?UTF-8?q?ree.redo.redo=5Fall=5F240721`=E5=8C=85=E4=B8=AD=E3=80=82?= =?UTF-8?q?=E5=85=B7=E4=BD=93=E9=97=AE=E9=A2=98=E5=92=8C=E8=A7=A3=E5=86=B3?= =?UTF-8?q?=E6=96=B9=E6=A1=88=E5=A6=82=E4=B8=8B=EF=BC=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. [LeetCode40.java](file://src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode40.java):解决组合总和II的问题,通过回溯算法找出所有不重复的组合。 2. [LeetCode90.java](file://src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode90.java):解决子集II的问题,同样使用回溯算法,找出所有可能的不重复子集。 3. [LeetCode131.java](file://src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode131.java):解决分割回文串的问题,提供两种解决方案:动态规划和回溯算法。 4. [LeetCode450.java](file://src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode450.java):解决删除二叉搜索树中的节点的问题,实现了一种删除节点的方法。 5. [LeetCode501.java](file://src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode501.java):找出二叉搜索树中的模式,通过遍历算法找出出现次数最多的元素。6. [LeetCode701.java](file://src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode701.java):解决向二叉搜索树插入节点的问题,提供了一个递归的解决方案。 这些解决方案都是针对LeetCode上的特定问题,目的是提供清晰、高效的代码实现,以便其他开发者可以学习和参考。 --- .../redo/redo_all_240721/LeetCode131.java | 114 ++++++++++++++++++ .../redo/redo_all_240721/LeetCode40.java | 56 +++++++++ .../redo/redo_all_240721/LeetCode450.java | 41 +++++++ .../redo/redo_all_240721/LeetCode501.java | 55 +++++++++ .../redo/redo_all_240721/LeetCode701.java | 33 +++++ .../redo/redo_all_240721/LeetCode90.java | 49 ++++++++ 6 files changed, 348 insertions(+) create mode 100644 src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode131.java create mode 100644 src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode40.java create mode 100644 src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode450.java create mode 100644 src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode501.java create mode 100644 src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode701.java create mode 100644 src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode90.java 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); + } + } +}