11. 递增子序列
题目链接:491. 递增子序列 - 力扣(LeetCode)
思路:递归回溯枚举所有组合:
- 注意去重(由于不能排序,那么我们用之前的去重条件剪枝,只能达到部分去重的目的,结果集仍然可能存在重复集合,如1 3 4 1 1),当枚举到这两个1时,结果集
[1, 1]
还是会重复的,为了达到去重的目的,我们可以用一个Set
集合来记录结果集。 - 要保证选择的序列是递增的
回溯三部曲
- 递归函数参数
public void dfs(int[] nums,int start){
}
- 终止条件
if(path.size()>=2){
res.add(new ArrayList<>(path));
}
- 单层搜索逻辑
利用HashSet
集合记录,避免重复,保证唯一。
for(int i = start;i < n;i++){
if(i>start&&nums[i]==nums[i-1]) continue;
if(path.size()>0&&nums[i]<path.get(path.size()-1)) continue;
path.add(nums[i]);
dfs(nums,i+1);
path.remove(path.size()-1);
}
Code
class Solution {
HashSet<List<Integer>> res = new HashSet<>();
List<Integer> path = new ArrayList<>();
int n;
public List<List<Integer>> findSubsequences(int[] nums) {
n = nums.length;
dfs(nums,0);
return new ArrayList<List<Integer>>(res);
}
public void dfs(int[] nums,int start){
if(path.size()>=2){
res.add(new ArrayList<>(path));
}
for(int i = start;i < n;i++){
if(i>start&&nums[i]==nums[i-1]) continue;
if(path.size()>0&&nums[i]<path.get(path.size()-1)) continue;
path.add(nums[i]);
dfs(nums,i+1);
path.remove(path.size()-1);
}
}
}
排列问题
12.全排列
思路:dfs实现排列型枚举
解法一:
回溯三部曲:
- 递归函数参数
private void dfs(int[] nums){
}
- 递归终止条件
if (path.size() == nums.length){
result.add(new ArrayList<>(path));
return;
}
- 单层搜索的逻辑
for (int i = 0; i < nums.length; i++){
if (used[i]){
continue;
}
used[i] = true;
path.add(nums[i]);
permuteHelper(nums);
path.removeLast();
used[i] = false;
}
Code
class Solution {
List<List<Integer>> result = new ArrayList<>();// 存放符合条件结果的集合
LinkedList<Integer> path = new LinkedList<>();// 用来存放符合条件结果
boolean[] used;
public List<List<Integer>> permute(int[] nums) {
if (nums.length == 0){
return result;
}
used = new boolean[nums.length];
dfs(nums);
return result;
}
private void dfs(int[] nums){
if (path.size() == nums.length){
result.add(new ArrayList<>(path));
return;
}
for (int i = 0; i < nums.length; i++){
if (used[i]){
continue;
}
used[i] = true;
path.add(nums[i]);
dfs(nums);
path.removeLast();
used[i] = false;
}
}
}
解法二:
// 解法2:通过判断path中是否存在数字,排除已经选择的数字
class Solution {
List<List<Integer>> result = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
public List<List<Integer>> permute(int[] nums) {
if (nums.length == 0) return result;
dfs(nums, path);
return result;
}
public void dfs(int[] nums, LinkedList<Integer> path) {
if (path.size() == nums.length) {
result.add(new ArrayList<>(path));
}
for (int i =0; i < nums.length; i++) {
// 如果path中已有,则跳过
if (path.contains(nums[i])) {
continue;
}
path.add(nums[i]);
dfs(nums, path);
path.removeLast();
}
}
}
13.全排列 II
思路:本题需要去重,来保证不取重复元素,我们选择树层去重。
回溯三部曲:
- 递归函数参数
private void dfs(int[] nums, boolean[] used) {
}
- 递归终止条件
if (path.size() == nums.length) {
result.add(new ArrayList<>(path));
return;
}
- 单层搜索的逻辑
for (int i = 0; i < nums.length; i++) {
// used[i - 1] == true,说明同⼀树⽀nums[i - 1]使⽤过
// used[i - 1] == false,说明同⼀树层nums[i - 1]使⽤过
// 如果同⼀树层nums[i - 1]使⽤过则直接跳过
if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false) {
continue;
}
//如果同⼀树⽀nums[i]没使⽤过开始处理
if (used[i] == false) {
used[i] = true;//标记同⼀树⽀nums[i]使⽤过,防止同一树枝重复使用
path.add(nums[i]);
dfs(nums, used);
path.remove(path.size() - 1);//回溯,说明同⼀树层nums[i]使⽤过,防止下一树层重复
used[i] = false;//回溯
}
Code:
class Solution {
//存放结果
List<List<Integer>> result = new ArrayList<>();
//暂存结果
List<Integer> path = new ArrayList<>();
public List<List<Integer>> permuteUnique(int[] nums) {
boolean[] used = new boolean[nums.length];
Arrays.fill(used, false);
Arrays.sort(nums);
dfs(nums, used);
return result;
}
private void dfs(int[] nums, boolean[] used) {
if (path.size() == nums.length) {
result.add(new ArrayList<>(path));
return;
}
for (int i = 0; i < nums.length; i++) {
// used[i - 1] == true,说明同⼀树⽀nums[i - 1]使⽤过
// used[i - 1] == false,说明同⼀树层nums[i - 1]使⽤过
// 如果同⼀树层nums[i - 1]使⽤过则直接跳过
if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false) {
continue;
}
//如果同⼀树⽀nums[i]没使⽤过开始处理
if (used[i] == false) {
used[i] = true;//标记同⼀树⽀nums[i]使⽤过,防止同一树枝重复使用
path.add(nums[i]);
dfs(nums, used);
path.remove(path.size() - 1);//回溯,说明同⼀树层nums[i]使⽤过,防止下一树层重复
used[i] = false;//回溯
}
}
}
}