提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
一、力扣491. 递增子序列
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
public List<List<Integer>> findSubsequences(int[] nums) {
// Arrays.sort(nums);
fun(nums, 0);
return res;
}
public void fun(int[] nums, int start){
if(path.size() >= 2){
res.add(new ArrayList<>(path));
}
if(start == nums.length){
return ;
}
HashSet<Integer> hs = new HashSet<>();
for(int i = start; i < nums.length; i ++){
if(hs.contains(nums[i])){
continue;
}
if(path.size()>=1 && path.get(path.size()-1) > nums[i]){
continue;
}
hs.add(nums[i]);
path.add(nums[i]);
fun(nums, i + 1);
path.remove(path.size()-1);
}
}
}
二、力扣47. 全排列 II
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
boolean[] flag;
public List<List<Integer>> permuteUnique(int[] nums) {
flag = new boolean[nums.length];
Arrays.fill(flag,false);
Arrays.sort(nums);
fun(nums);
return res;
}
public void fun(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] == nums[i-1] && !flag[i-1]){
continue;
}
if(flag[i]==false){
flag[i] = true;
path.add(nums[i]);
fun(nums);
path.remove(path.size()-1);
flag[i] = false;
}
}
}
}
三、力扣46. 全排列
class Solution {
List<List<Integer>> res = new ArrayList<>();
boolean[] flag;
List<Integer> path = new ArrayList<>();
public List<List<Integer>> permute(int[] nums) {
flag = new boolean[nums.length];
Arrays.fill(flag, false);
fun(nums);
return res;
}
public void fun(int[] nums){
if(nums.length == path.size()){
res.add(new ArrayList<>(path));
return;
}
for(int i = 0; i < nums.length; i ++){
if(flag[i] == false){
flag[i] = true;
path.add(nums[i]);
fun(nums);
flag[i] = false;
path.remove(path.size()-1);
}
}
}
}