From 17bbb0f3ed1c955ae0df574bed2a87d9c1c8af5c Mon Sep 17 00:00:00 2001 From: whai Date: Mon, 19 Feb 2024 10:48:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=9E=E6=BA=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leetCode/BackTracking/LeetCode491.java | 59 ++++++ .../leetCode/BackTracking/LeetCode78.java | 44 +++++ .../leetCode/BackTracking/LeetCode90.java | 66 +++++++ .../leetCode/BackTracking/LeetCode93.java | 176 ++++++++++++++++++ .../redo/redo_24_2_18/LeetCode131.java | 67 +++++++ 5 files changed, 412 insertions(+) create mode 100644 src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode491.java create mode 100644 src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode78.java create mode 100644 src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode90.java create mode 100644 src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode93.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_2_18/LeetCode131.java diff --git a/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode491.java b/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode491.java new file mode 100644 index 0000000..51f3ac1 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode491.java @@ -0,0 +1,59 @@ +package cn.whaifree.leetCode.BackTracking; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/2/18 16:41 + * @注释 + */ +public class LeetCode491 { + + @Test + public void test() { + new Solution().findSubsequences(new int[]{4,6,7,5,7}).forEach(list -> { + System.out.println(list); + }); + } + + class Solution { + + List> res = new ArrayList<>(); + List path = new ArrayList<>(); + public List> findSubsequences(int[] nums) { + + backTracking(nums, 0); + return res; + } + + public void backTracking(int[] nums, int index) { + + if (path.size() > 1) { + // 插入res + res.add(new ArrayList<>(path)); + } + + Set set = new HashSet<>(); + for (int i = index; i < nums.length; i++) { + // 如果不满足非递减,即递增,这个子树不保留 + if ((!path.isEmpty() && path.get(path.size() - 1) > nums[i]) // 递增 + || set.contains(nums[i]) // 如[4,7,xx,7],[4,7]已经出现在path中,7加入了set,那么在本层后面遇到7的时候,就直接continues + ) { + continue; + } + set.add(nums[i]); + path.add(nums[i]); + backTracking(nums, i + 1); + path.remove(path.size() - 1); + } + + } + } + +} diff --git a/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode78.java b/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode78.java new file mode 100644 index 0000000..33bcc2e --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode78.java @@ -0,0 +1,44 @@ +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/18 10:29 + * @注释 + */ +public class LeetCode78 { + + @Test + public void test() { + System.out.println(new Solution().subsets(new int[]{1, 2, 3})); + } + + class Solution { + + List> res = new ArrayList<>(); + List temp = new ArrayList<>(); + public List> subsets(int[] nums) { + backTracking(nums, 0); + return res; + } + + void backTracking(int[] nums, int start) { + res.add(new ArrayList<>(temp)); + if (start >= nums.length) { + return; + } + + for (int i = start; i < nums.length ; i++) { + temp.add(nums[i]); + backTracking(nums,i+1); + temp.remove(temp.size() - 1); + } + + } + } +} diff --git a/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode90.java b/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode90.java new file mode 100644 index 0000000..e07b6a0 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode90.java @@ -0,0 +1,66 @@ +package cn.whaifree.leetCode.BackTracking; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/2/18 13:26 + * @注释 + */ +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<>(); + List temp = new ArrayList<>(); + boolean[] used = null; + public List> subsetsWithDup(int[] nums) { + Arrays.sort(nums); + used = new boolean[nums.length]; + backTracking(nums, 0); + return res; + } + + void backTracking(int[] nums, int start) { + res.add(new ArrayList<>(temp)); + if (start >= nums.length) { + return; + } + + + + for (int i = start; i < nums.length ; i++) { + + // 1. u1 u2 相同,并且u1曾经被调用过,则不继续进行本次循环 +// if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false) { +// continue; +// } + // 2. 不使用used数组,跳过当前树层使用过的、相同的元素 + if ( i > start && nums[i - 1] == nums[i] ) { + continue; + } + + temp.add(nums[i]); + used[i] = true; + backTracking(nums,i+1); + temp.remove(temp.size() - 1); + used[i] = false; + } + + } + } +} diff --git a/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode93.java b/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode93.java new file mode 100644 index 0000000..6096a72 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode93.java @@ -0,0 +1,176 @@ +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/18 9:27 + * @注释 + */ +public class LeetCode93 { + + @Test + public void test() { + for (String restoreIpAddress : new Solution1().restoreIpAddresses("101023")) { + System.out.println(restoreIpAddress); + } + } + + class Solution { + + List res = new ArrayList<>(); + List path = new ArrayList<>(); + public List restoreIpAddresses(String s) { + backTracking(s, 0, 0); + return res; + } + + /** + * + * @param s + * @param start 递归开始标记 + * @param number 这是第几个切片,4个切片为一组有效返回 + */ + public void backTracking(String s, int start,int number) { + // 如果 第5个切片了,则存在多余的字符,直接return + if (number > 4) { + return; + } + + if (start >= s.length() && path.size() == 4) { + res.add(String.join(".", path)); + +// StringBuilder stringBuilder = new StringBuilder(); +// for (int i = 0; i < path.size() - 1; i++) { +// stringBuilder.append(path.get(i)).append("."); +// } +// stringBuilder.append(path.get(path.size() - 1)); +// res.add(stringBuilder.toString()); + return; + } + + + + + for (int i = start; i < s.length() ; i++) { + + if (isValid(s, start, i + 1)) { + + path.add(s.substring(start, i + 1)); + backTracking(s, i + 1, number + 1); + path.remove(path.size() - 1); + }else { + // 2565 256无效,那么2565直接就无效了 + break; + } + } + } + + public boolean isValid(String s, int start, int end) { + String substring = s.substring(start, end); + // 子串最多不超过3个字符 + if (substring.length() > 3 ) { + return false; + } + // “01”为非法,"0"为合法 + if (substring.length() != 1 && substring.startsWith("0")) { + return false; + } + int integer = Integer.parseInt(substring); + if (integer >= 0 && integer <= 255) { + return true; + } + return false; + } + + /** + * 通过char进行字符串转为int + * @param s + * @param start + * @param end + * @return + */ + private boolean isValid(StringBuilder s, int start, int end){ + if(start > end) + return false; + if(s.charAt(start) == '0' && start != end) + return false; + int num = 0; + for(int i = start; i <= end; i++){ + int digit = s.charAt(i) - '0'; + num = num * 10 + digit; + if(num > 255) + return false; + } + return true; + } + } + class Solution1 { + + List res = new ArrayList<>(); + List path = new ArrayList<>(); + public List restoreIpAddresses(String s) { + backTracking(s, 0); + return res; + } + + /** + * + * @param s + * @param start 递归开始标记 + */ + public void backTracking(String s, int start) { + + if (start >= s.length() && path.size() == 4) { + res.add(String.join(".", path)); + +// StringBuilder stringBuilder = new StringBuilder(); +// for (int i = 0; i < path.size() - 1; i++) { +// stringBuilder.append(path.get(i)).append("."); +// } +// stringBuilder.append(path.get(path.size() - 1)); +// res.add(stringBuilder.toString()); + return; + } + + + + + for (int i = start; i < s.length() ; i++) { + + if (isValid(s, start, i + 1)) { + + path.add(s.substring(start, i + 1)); + backTracking(s, i + 1); + path.remove(path.size() - 1); + }else { + // 2565 256无效,那么2565直接就无效了 + break; + } + } + } + + public boolean isValid(String s, int start, int end) { + String substring = s.substring(start, end); + // 子串最多不超过3个字符 + if (substring.length() > 3 ) { + return false; + } + // “01”为非法,"0"为合法 + if (substring.length() != 1 && substring.startsWith("0")) { + return false; + } + int integer = Integer.parseInt(substring); + if (integer >= 0 && integer <= 255) { + return true; + } + return false; + } + + } + +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_2_18/LeetCode131.java b/src/main/java/cn/whaifree/redo/redo_24_2_18/LeetCode131.java new file mode 100644 index 0000000..bc0735e --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_2_18/LeetCode131.java @@ -0,0 +1,67 @@ +package cn.whaifree.redo.redo_24_2_18; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/2/18 9:04 + * @注释 + */ +public class LeetCode131 { + + @Test + + public void test() { + String abc = "aab"; + new Solution().partition(abc).forEach( + list -> { + System.out.println(list); + } + ); + } + + class Solution { + + List> res = new ArrayList<>(); + List list = new ArrayList<>(); + + + public List> partition(String s) { + backTracing(s,0); + return res; + } + + public void backTracing(String s,int start) { + + // 遍历完了,树到根部了,就记为一次结果 + if (start >= s.length()) { + res.add(new ArrayList<>(list)); + } + + for (int i = start; i < s.length(); i++) { + // 只有当是回文串时,才进行下一次切割 + if (isHuiWen(s, start, i)) { + String substring = s.substring(start, i + 1); + list.add(substring); + backTracing(s, i + 1); + list.remove(list.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; + } + } +}