代码随想录算法训练营第二十五天-491递增子序列 、46全排列 、47全排列 II
1 2
| 24-06-11 初始记录 24-06-12 完成
|
初步题解
491 递增子序列
题目链接:(https://leetcode.cn/problems/non-decreasing-subsequences)
超时了,主要是去重那一步应该。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| public static List<List<Integer>> findSubsequences(int[] nums) { List<List<Integer>> result = new ArrayList<>(); List<Integer> resultOne = new ArrayList<>(); findSubsequencesDFS(0 , nums, resultOne, result); return result; } private static void findSubsequencesDFS(int startIndex, int[] nums, List<Integer> resultOne, List<List<Integer>> result) { if (resultOne.size() >= 2 && !result.contains(resultOne)) { result.add(new ArrayList<>(resultOne)); } for (int i = startIndex; i < nums.length; i++) { if (resultOne.size() != 0 && resultOne.get(resultOne.size() - 1) > nums[i]) { continue; } resultOne.add(nums[i]); findSubsequencesDFS(i + 1, nums, resultOne, result); resultOne.remove(resultOne.size() - 1); } }
|
46 全排列
题目链接:(https://leetcode.cn/problems/permutations/description/)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| public static List<List<Integer>> permute(int[] nums) { List<List<Integer>> result = new ArrayList<>(); List<Integer> resultOne = new ArrayList<>(); if (nums == null || nums.length == 0) { return result; } permuteDFS(nums, resultOne, result); return result; } private static void permuteDFS(int[] nums, List<Integer> resultOne, List<List<Integer>> result) { if (resultOne.size() == nums.length) { result.add(new ArrayList<>(resultOne)); return; } for (int i = 0; i < nums.length; i++) { if (resultOne.contains(nums[i])) { continue; } resultOne.add(nums[i]); permuteDFS(nums, resultOne, result); resultOne.remove(resultOne.size() - 1); } }
|
47 全排列 II
题目链接:(leetcode.cn/problems/permutations-ii/)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
public static List<List<Integer>> permuteUnique(int[] nums) { List<List<Integer>> result = new ArrayList<>(); List<Integer> resultOne = new ArrayList<>(); if (nums == null || nums.length == 0) { return result; } Arrays.sort(nums); permuteUniqueDFS(nums, new ArrayList<>(), resultOne, result); return result; } private static void permuteUniqueDFS(int[] nums, List<Integer> resultIndex, List<Integer> resultOne, List<List<Integer>> result) { if (resultOne.size() == nums.length) { result.add(new ArrayList<>(resultOne)); return; } HashSet<Integer> set = new HashSet<>(); for (int i = 0; i < nums.length; i++) { if (resultIndex.contains(i)) { continue; } if (set.contains(nums[i])) { continue; } resultIndex.add(i); resultOne.add(nums[i]); set.add(nums[i]); permuteUniqueDFS(nums, resultIndex, resultOne, result); resultOne.remove(resultOne.size() - 1); resultIndex.remove(resultIndex.size() - 1); } }
|
看题解
491 递增子序列
(https://programmercarl.com/0491.递增子序列.html)
视频讲解:(https://www.bilibili.com/video/BV1EG4y1h78v)
加了剪枝操作的优化。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| public static List<List<Integer>> findSubsequences(int[] nums) { List<List<Integer>> result = new ArrayList<>(); List<Integer> resultOne = new ArrayList<>(); findSubsequencesDFS(0 , nums, resultOne, result); return result; } private static void findSubsequencesDFS(int startIndex, int[] nums, List<Integer> resultOne, List<List<Integer>> result) { if (resultOne.size() >= 2) { result.add(new ArrayList<>(resultOne)); } HashMap<Integer,Integer> map = new HashMap<>(); for (int i = startIndex; i < nums.length; i++) { if (resultOne.size() != 0 && resultOne.get(resultOne.size() - 1) > nums[i]) { continue; } if (map.getOrDefault(nums[i], 0) >= 1) { continue; } map.put(nums[i], map.getOrDefault(nums[i], 0) + 1); resultOne.add(nums[i]); findSubsequencesDFS(i + 1, nums, resultOne, result); resultOne.remove(resultOne.size() - 1); } }
|
46 全排列
(https://programmercarl.com/0046.全排列.html)
视频讲解:(https://www.bilibili.com/video/BV19v4y1S79W)
47 全排列 II
(https://programmercarl.com/0047.全排列II.html)
视频讲解:(https://www.bilibili.com/video/BV1R84y1i7Tm)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| public static List<List<Integer>> permuteUnique(int[] nums) { List<List<Integer>> result = new ArrayList<>(); List<Integer> resultOne = new ArrayList<>(); if (nums == null || nums.length == 0) { return result; } Arrays.sort(nums); permuteUniqueDFS(nums, new boolean[]{}, resultOne, result); return result; }
private static void permuteUniqueDFS(int[] nums, boolean[] used, List<Integer> resultOne, List<List<Integer>> result) { if (resultOne.size() == nums.length) { result.add(new ArrayList<>(resultOne)); return; } for (int i = 0; i < nums.length; i++) { if (used[i]) { continue; } if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]) { continue; } used[i] = true; resultOne.add(nums[i]); permuteUniqueDFS(nums, used, resultOne, result); resultOne.remove(resultOne.size() - 1); used[i] = false; } }
|