From b8be07776c953eef75edd67b0147b55b8ffa0531 Mon Sep 17 00:00:00 2001 From: whai Date: Sat, 24 Feb 2024 12:56:53 +0800 Subject: [PATCH] redo --- .../redo/redo_24_2_22/LeetCode131.java | 70 ++++++++++------- .../redo/redo_24_2_22/LeetCode236.java | 50 +++++++++++++ .../redo/redo_24_2_22/LeetCode46.java | 75 +++++++++++++++++++ .../redo/redo_24_2_22/LeetCode47.java | 58 ++++++++++++++ .../redo/redo_24_2_22/LeetCode491.java | 59 +++++++++++++++ .../redo/redo_24_2_22/LeetCode90.java | 58 ++++++++++++++ 6 files changed, 342 insertions(+), 28 deletions(-) create mode 100644 src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode236.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode46.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode47.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode491.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode90.java diff --git a/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode131.java b/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode131.java index a6ae63d..72a1202 100644 --- a/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode131.java +++ b/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode131.java @@ -1,50 +1,64 @@ package cn.whaifree.redo.redo_24_2_22; -import cn.whaifree.leetCode.model.TreeNode; import org.junit.Test; +import org.w3c.dom.ls.LSException; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; /** * @version 1.0 * @Author whai文海 - * @Date 2024/2/22 11:25 + * @Date 2024/2/23 11:56 * @注释 */ public class LeetCode131 { @Test - public void test( - ) { - TreeNode treeNode = TreeNode.constructTreeByArray(3, 5, 1, 6, 2, 0, 8, null, null, 7, 4); + public void test() { + new Solution().partition("aab").forEach( + list -> { + System.out.println(list); + } + ); } + class Solution { - public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { - if (root == null) { - return root; + List> res = new ArrayList<>(); + LinkedList path = new LinkedList<>(); + public List> partition(String s) { + backTracking(s, 0, s.length() - 1); + return res; + } + + public void backTracking(String s, int start, int end) { + + if (start >= s.length()) { + res.add(new ArrayList<>(path)); + return; } - if (root.val == p.val) { - return root; - } - if (root.val == q.val) { - return root; - } - - TreeNode right = lowestCommonAncestor(root.right, p, q); - TreeNode left = lowestCommonAncestor(root.left, p, q); - - // 这里用于输出公共父节点 - // 某个节点,第一个出现的左边有p或者q,右边也有p或者q,则就是公共父节点。 - if (right != null && left != null) { - return root; - } - - if (right != null) { - return right; - } else { - return left; + for (int i = start; i < s.length(); i++) { + if (isHuiWen(s, start, i)) { + path.add(s.substring(start, i + 1)); + backTracking(s, i + 1, end); + path.removeLast(); + } } } + + 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_24_2_22/LeetCode236.java b/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode236.java new file mode 100644 index 0000000..c5f9b9e --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode236.java @@ -0,0 +1,50 @@ +package cn.whaifree.redo.redo_24_2_22; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/2/22 11:25 + * @注释 + */ +public class LeetCode236 { + + @Test + public void test( + ) { + TreeNode treeNode = TreeNode.constructTreeByArray(3, 5, 1, 6, 2, 0, 8, null, null, 7, 4); + } + + class Solution { + public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { + if (root == null) { + return root; + } + + if (root.val == p.val) { + return root; + } + if (root.val == q.val) { + return root; + } + + TreeNode right = lowestCommonAncestor(root.right, p, q); + TreeNode left = lowestCommonAncestor(root.left, p, q); + + // 这里用于输出公共父节点 + // 某个节点,第一个出现的左边有p或者q,右边也有p或者q,则就是公共父节点。 + if (right != null && left != null) { + return root; + } + + if (right != null) { + return right; + } else { + return left; + } + + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode46.java b/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode46.java new file mode 100644 index 0000000..a75f506 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode46.java @@ -0,0 +1,75 @@ +package cn.whaifree.redo.redo_24_2_22; + +import org.junit.Test; + +import java.lang.management.ManagementFactory; +import java.lang.management.ThreadInfo; +import java.lang.management.ThreadMXBean; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/2/23 13:19 + * @注释 + */ +public class LeetCode46 { + + @Test + public void test() { + new Solution().permute(new int[]{1, 2, 3}).forEach( + list -> { + System.out.println(list); + } + ); + } + + class Solution + { + + List> res = new ArrayList<>(); + LinkedList path = new LinkedList<>(); + boolean[] used = null; + public List> permute(int[] nums) { + used = new boolean[nums.length]; + backTracking(nums); + return res; + } + + void backTracking(int[] nums) { + if (path.size() == nums.length) { + res.add(new ArrayList<>(path)); + return; + } + + for (int i = 0; i < nums.length; i++) { + // 跳过子树路径上同一个 + if (used[i] == true) { + continue; + } + path.add(nums[i]); + used[i] = true; + backTracking(nums); + used[i] = false; + path.removeLast(); + } + + + } + } + + public static void main(String[] args) { + // 获取 Java 线程管理 MXBean + ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); + // 不需要获取同步的 monitor 和 synchronizer 信息,仅获取线程和线程堆栈信息 + ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(false, false); + // 遍历线程信息,仅打印线程 ID 和线程名称信息 + for (ThreadInfo threadInfo : threadInfos) { + System.out.println("[" + threadInfo.getThreadId() + "] " + threadInfo.getThreadName()); + } + } + +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode47.java b/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode47.java new file mode 100644 index 0000000..c796f3b --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode47.java @@ -0,0 +1,58 @@ +package cn.whaifree.redo.redo_24_2_22; + +import org.junit.Test; + +import java.util.*; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/2/23 13:43 + * @注释 + */ +public class LeetCode47 { + @Test + public void test() { + new Solution().permuteUnique(new int[]{1,1,2}).forEach( + list -> { + System.out.println(list); + } + ); + } + + class Solution { + List> res = new ArrayList<>(); + LinkedList path = new LinkedList<>(); + boolean[] used = null; + public List> permuteUnique(int[] nums) { + Arrays.sort(nums); + used = new boolean[nums.length]; + backTracking(nums); + return res; + } + + public void backTracking(int[] nums) { + + if (path.size() == nums.length) { + res.add(new ArrayList<>(path)); + return; + } + + for (int i = 0; i < nums.length; i++) { + if ((i > 0 && nums[i] == nums[i - 1] && used[i-1] == false)) { + continue; + } + + if (used[i]) { + continue; + } + used[i] = true; + path.add(nums[i]); + backTracking(nums); + used[i] = false; + path.removeLast(); + } + + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode491.java b/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode491.java new file mode 100644 index 0000000..4ccdc39 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode491.java @@ -0,0 +1,59 @@ +package cn.whaifree.redo.redo_24_2_22; + +import org.junit.Test; + +import java.util.*; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/2/23 12:10 + * @注释 + */ +public class LeetCode491 { + + @Test + public void test() { + new Solution().findSubsequences(new int[]{1, 5, 3, 3}).forEach( + list -> { + System.out.println(list); + } + ); + } + + + class Solution { + List> res = new ArrayList<>(); + LinkedList path = new LinkedList<>(); + public List> findSubsequences(int[] nums) { + backTracking(nums, 0); + return res; + } + + + public void backTracking(int[] nums, int start) { + if (path.size() > 1) { + // 插入res + res.add(new ArrayList<>(path)); + } + + // 每层跳过,不能排序,所以不能使用used + Set set = new HashSet<>(); + for (int i = start; i < nums.length; i++) { + // 如果 递减,跳过 + if ((!path.isEmpty() && path.getLast() > nums[i]) || set.contains(nums[i])) { + continue; + } + + set.add(nums[i]); + path.add(nums[i]); + backTracking(nums, i + 1); + path.removeLast(); + } + + + } + } + + +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode90.java b/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode90.java new file mode 100644 index 0000000..033547f --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode90.java @@ -0,0 +1,58 @@ +package cn.whaifree.redo.redo_24_2_22; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/2/23 10:02 + * @注释 + */ +public class LeetCode90 { + @Test + public void test() { + new Solution().subsetsWithDup(new int[]{1, 2, 2}).forEach( + list -> { + System.out.println(list); + } + ); + } + + class Solution { + + List> res = new ArrayList<>(); + LinkedList path = new LinkedList<>(); + boolean[] used = null; + public List> subsetsWithDup(int[] nums) { + used = new boolean[nums.length]; + Arrays.sort(nums); + backTracking(nums, 0); + return res; + } + + public void backTracking(int[] nums, int start) { + res.add(new ArrayList<>(path)); + if (start >= nums.length) { + return; + } + + for (int i = start; i < nums.length; i++) { + // 兄弟节点被使用完了,就是false,continue + if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false) { + continue; + } + + used[i] = true; + path.add(nums[i]); + backTracking(nums, i + 1); + used[i] = false; + path.removeLast(); + } + } + } +}