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; + } + } +}