Day 29 回溯算法part05
今日任务
- 491.递增子序列
- 46.全排列
- 47.全排列 II
代码实现
491.递增子序列
public List<List<Integer>> findSubsequences(int[] nums) {List<List<Integer>> result = new ArrayList<>();List<Integer> path = new ArrayList<>();backtracking(nums, 0, path, result);return result;}void backtracking(int[] nums, int startIndex, List<Integer> path, List<List<Integer>> result) {if (path.size() > 1) {result.add(new ArrayList<>(path));}HashSet<Integer> set = new HashSet<>();for (int i = startIndex; i < nums.length; i++) {if ((path.size() > 0 && nums[i] < path.get(path.size() - 1)) || set.contains(nums[i])) {continue;}set.add(nums[i]);path.add(nums[i]);backtracking(nums, i + 1, path, result);path.remove(path.size() - 1);}}
46.全排列
这里我没有用标记已使用的方法,而是用path.contains判断,结果到下一道题就行不通了
public List<List<Integer>> permute(int[] nums) {List<List<Integer>> result = new ArrayList<>();List<Integer> path = new ArrayList<>();backtracking(nums, path, result);return result;}void backtracking(int[] nums, List<Integer> path, List<List<Integer>> result) {if (path.size() == nums.length) {result.add(new ArrayList<>(path));return;}for (int num : nums) {if (path.contains(num)) {continue;}path.add(num);backtracking(nums, path, result);path.remove(path.size() - 1);}}
47.全排列 II
重点在于如何去重,可以通过排序+used数组去重
public List<List<Integer>> permuteUnique(int[] nums) {List<List<Integer>> result = new ArrayList<>();List<Integer> path = new ArrayList<>();boolean[] used = new boolean[nums.length];backtracking1(nums, path, result, used);return result;}void backtracking1(int[] nums, List<Integer> path, List<List<Integer>> result, boolean[] used) {if (path.size() == nums.length) {result.add(new ArrayList<>(path));return;}HashSet<Integer> set = new HashSet<>();for (int i = 0; i < nums.length; i++) {if (used[i] || set.contains(nums[i])) {continue;}set.add(nums[i]);used[i] = true;path.add(nums[i]);backtracking1(nums, path, result, used);path.remove(path.size() - 1);used[i] = false;}}
今日总结
- 这里有两种去重方式,一种是通过used数组去重,这种方法的关键是数组需要排序,还有一种是在for循环中用一个set去重;去重的关键是要分清树枝去重和树层去重,for循环代表一层,递归代表往下;
- 因为有公式的存在,很多题其实想不太明白,只是套公式,然后根据用例通不过的地方去修改,就解决了,而对于回溯本身想的不是很清楚,递归本身也比较抽象;
- 今天小绿,又追高了,真是愚蠢
- 另外,听说最近工作不好找,我还是得试试啊。