From b20f5f042fab0e696fa23efd38d627d8a85c198e Mon Sep 17 00:00:00 2001 From: whaifree <49432110+whaibetter@users.noreply.github.com> Date: Sun, 4 Aug 2024 16:37:30 +0800 Subject: [PATCH] =?UTF-8?q?update=20whaifree=20redo=20demo=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E4=BA=86whaifree=E9=87=8D=E5=81=9A=E6=BC=94=E7=A4=BA?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=EF=BC=8C=E6=96=B0=E5=A2=9E=E4=BA=86=E5=87=A0?= =?UTF-8?q?=E4=B8=AALeetCode=E9=A2=98=E7=9B=AE=EF=BC=8846=E3=80=8147?= =?UTF-8?q?=E3=80=8151=E3=80=81491=EF=BC=89=E7=9A=84=E8=A7=A3=E5=86=B3?= =?UTF-8?q?=E6=96=B9=E6=A1=88=EF=BC=8C=E4=BF=AE=E6=94=B9=E4=BA=8640?= =?UTF-8?q?=E9=A2=98=E7=9A=84=E8=A7=A3=E6=B3=95=EF=BC=8C=E5=8C=85=E6=8B=AC?= =?UTF-8?q?=E5=9B=9E=E6=BA=AF=E7=AE=97=E6=B3=95=E7=9A=84=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E5=92=8C=E4=B8=80=E4=BA=9B=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= =?UTF-8?q?=E7=9A=84=E8=BE=93=E5=87=BA=E6=96=B9=E5=BC=8F=E3=80=82=E7=A1=AE?= =?UTF-8?q?=E4=BF=9D=E4=BA=86=E4=BB=A3=E7=A0=81=E7=9A=84=E6=AD=A3=E7=A1=AE?= =?UTF-8?q?=E6=80=A7=E5=92=8C=E5=8F=AF=E8=AF=BB=E6=80=A7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../redo/redo_all_240721/LeetCode40.java | 52 ++++++- .../redo/redo_all_240721/LeetCode46.java | 48 ++++++ .../redo/redo_all_240721/LeetCode47.java | 89 +++++++++++ .../redo/redo_all_240721/LeetCode491.java | 146 ++++++++++++++++++ .../redo/redo_all_240721/LeetCode51.java | 85 ++++++++++ 5 files changed, 412 insertions(+), 8 deletions(-) create mode 100644 src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode46.java create mode 100644 src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode47.java create mode 100644 src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode491.java create mode 100644 src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode51.java 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 index 923e23d..5f8ddcd 100644 --- a/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode40.java +++ b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode40.java @@ -11,14 +11,9 @@ import java.util.*; 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(); - } - ); + new Solution().combinationSum2(new int[]{10, 1, 2, 7, 6, 1, 5}, 8).forEach(list -> { + System.out.println(list); + }); } List> res = new ArrayList<>(); @@ -53,4 +48,45 @@ public class LeetCode40 { nowSum -= candidates[i]; } } + + + static class Solution { + + List> res = new ArrayList<>(); + List path = new ArrayList<>(); + + /** + * + * + * @param candidates + * @param target + * @return + */ + public List> combinationSum2(int[] candidates, int target) { + Arrays.sort(candidates); + backTracking(candidates, 0, target); + return res; + } + + public void backTracking(int[] candidates, int index, int need) { + if (need < 0) { + return; + } + if (need == 0) { + res.add(new ArrayList<>(path)); + } + HashSet set = new HashSet<>(); + for (int i = index; i < candidates.length; i++) { + if (set.contains(candidates[i])) { + continue; + } + set.add(candidates[i]); + path.add(candidates[i]); + backTracking(candidates, i + 1, need - candidates[i]); + path.remove(path.size() - 1); + } + } + + } + } diff --git a/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode46.java b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode46.java new file mode 100644 index 0000000..1d41bb6 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode46.java @@ -0,0 +1,48 @@ +package cn.whaifree.redo.redo_all_240721; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/3 10:19 + * @注释 + */ +public class LeetCode46 { + + @Test + public void test() { + int[] nums = {1,2,3}; + Solution solution = new Solution(); + solution.permute(nums).forEach( + System.out::println + ); + } + class Solution { + List> res = new ArrayList<>(); + List path = new ArrayList<>(); + public List> permute(int[] nums) { + back(nums); + return res; + } + + public void back(int[] nums) { + if (path.size() >= nums.length) { + res.add(new ArrayList<>(path)); + return; + } + + for (int i = 0; i < nums.length; i++) { + if (path.contains(nums[i])) { + continue; + } + path.add(nums[i]); + back(nums); + path.remove(path.size() - 1); + } + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode47.java b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode47.java new file mode 100644 index 0000000..c772717 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode47.java @@ -0,0 +1,89 @@ +package cn.whaifree.redo.redo_all_240721; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/3 13:15 + * @注释 + */ +public class LeetCode47 { + @Test + public void test() + { + int[] nums = new int[]{1, 1, 2}; + List> x = new Solution1().permuteUnique(nums); + for (List integers : x) { + System.out.println(integers); + } + } + + class Solution { + + List> res = new ArrayList<>(); + List path = new ArrayList<>(); + public List> permuteUnique(int[] nums) { + back(nums); + return res; + } + + public void back(int[] nums) { + if (path.size() >= nums.length) { + res.add(path.stream().mapToInt(value -> nums[value]).boxed().toList()); + return; + } + HashSet set = new HashSet<>(); + for (int i = 0; i < nums.length; i++) { + if (path.contains(i)) { + continue; + } + if (set.contains(nums[i])) { + continue; + } + set.add(nums[i]); + path.add(i); + back(nums); + path.remove(path.size() - 1); + } + } + } + + class Solution1 { + + List> res = new ArrayList<>(); + List path = new ArrayList<>(); + boolean[] used = null; + public List> permuteUnique(int[] nums) { + used = new boolean[nums.length]; + Arrays.sort(nums); + back(nums); + return res; + } + + public void back(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 - 1] == nums[i] && !used[i - 1]) { + continue; + } + if (used[i]) { // 每条路径,同一个元素不会再使用 + continue; + } + used[i] = true; + path.add(nums[i]); + back(nums); + used[i] = false; + path.remove(path.size() - 1); + } + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode491.java b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode491.java new file mode 100644 index 0000000..0ca8d53 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode491.java @@ -0,0 +1,146 @@ +package cn.whaifree.redo.redo_all_240721; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/2 23:57 + * @注释 + */ +public class LeetCode491 { + + public static void main(String[] args) { + new Solution().findSubsequences(new int[]{4, 6, 7, 7}).forEach(System.out::println); +// new Solution1().subsetsWithDup(new int[]{1,2,2}).forEach(System.out::println); + } + + static class Solution { + List> res; + List path; + public List> findSubsequences(int[] nums) { + res = new ArrayList<>(); + path = new ArrayList<>(); + back(0, nums); + return res; + } + + /** + * 4677 + * 4 + * 6 + * 7 7 这两个要去重,在这层用set + * + * + * + * @param start + * @param nums + */ + public void back(int start, int[] nums) { + if (path.size() > 1) { + res.add(new ArrayList<>(path)); + } + if (start >= nums.length) { + return; + } + HashSet set = new HashSet<>(); + for (int i = start; i < nums.length; i++) { + if (set.contains(nums[i])) { + continue; + } + if (path.isEmpty() || path.get(path.size() - 1) <= nums[i]) { + path.add(nums[i]); + set.add(nums[i]); + back(i + 1, nums); + path.remove(path.size() - 1); + } + } + } + } +// 错误,used只能用在相邻的判断 +// static class Solution2 { +// List> res; +// List path; +// boolean[] used = null; +// public List> findSubsequences(int[] nums) { +// res = new ArrayList<>(); +// path = new ArrayList<>(); +// used = new boolean[nums.length]; +// back(0, nums); +// return res; +// } +// +// /** +// * 4677 +// * 4 +// * 6 +// * 7 7 这两个要去重,在这层用set +// * +// * +// * +// * @param start +// * @param nums +// */ +// public void back(int start, int[] nums) { +// if (path.size() > 1) { +// res.add(new ArrayList<>(path)); +// } +// if (start >= nums.length) { +// return; +// } +// for (int i = start; i < nums.length; i++) { +// if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false) { +// continue; +// } +// if (path.isEmpty() || path.get(path.size() - 1) <= nums[i]) { +// path.add(nums[i]); +// used[i] = true; +// back(i + 1, nums); +// used[i] = false; +// path.remove(path.size() - 1); +// } +// } +// } +// } + + + static class Solution1 { + + 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++) { + + // u1 u2 相同,并且u1曾经被调用过,则不继续进行本次循环 +// if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false) { +// 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/redo/redo_all_240721/LeetCode51.java b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode51.java new file mode 100644 index 0000000..61aa152 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode51.java @@ -0,0 +1,85 @@ +package cn.whaifree.redo.redo_all_240721; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/3 13:34 + * @注释 + */ +public class LeetCode51 { + @Test + public void test() + { + Solution solution = new Solution(); + int n = 4; + System.out.println(solution.solveNQueens(n)); + } + + class Solution { + + char[][] map; + List> res = new ArrayList<>(); + + /** + * + * @param col 确定下来,下次递归 + * @param n + */ + public void back(int col, int n) { + if (col == n) { + List path = new ArrayList<>(); + for (char[] chars : map) { + String s = new String(chars); + path.add(s); + } + res.add(path); + return; + } + + for (int i = 0; i < n; i++) { + if (isValid(col, i, n)) { + map[i][col] = 'Q'; + back(col + 1, n); + map[i][col] = '.'; + } + } + } + public List> solveNQueens(int n) { + map = new char[n][n]; + for (char[] chars : map) { + Arrays.fill(chars, '.'); + } + back(0, n); + return res; + } + + boolean isValid(int col, int row, int n) { + + // 往左查找 + for (int i = 0; i < col; i++) { + if (map[row][i] == 'Q') { + return false; + } + } + // 左上45 + for (int i = col, j = row; i >= 0 && j >= 0; i--, j--) { + if (map[j][i] == 'Q') { + return false; + } + } + // 左下45 + for (int i = col, j = row; i >= 0 && j < n; i--, j++) { + if (map[j][i] == 'Q') { + return false; + } + } + return true; + } + } +}