提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
// boolean[] flag ;
public List<List<Integer>> subsets(int[] nums) {
// flag = new boolean[nums.length];
fun(nums,0);
return res;
}
public void fun(int[] nums,int index){
res.add(new ArrayList<>(path));
for(int i = index; i < nums.length; i ++){
path.add(nums[i]);
fun(nums,i+1);
path.remove(path.size()-1);
}
}
}
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
public List<List<Integer>> combine(int n, int k) {
fun(n, k, 1);
return res;
}
public void fun(int n, int k, int index){
if(path.size() == k){
res.add(new ArrayList<>(path));
return;
}
for(int i = index; i <= n; i ++){
path.add(i);
fun(n, k, i + 1);
path.remove(path.size()-1);
}
}
}
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
boolean[] flag;
public List<List<Integer>> permute(int[] nums) {
flag = new boolean[nums.length];
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(flag[i] == false){
flag[i] = true;
path.add(nums[i]);
fun(nums);
path.remove(path.size()-1);
flag[i] = false;
}
}
}
}
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
boolean[] flag;
public List<List<Integer>> subsetsWithDup(int[] nums) {
flag = new boolean[nums.length];
Arrays.sort(nums);
fun(nums,0);
return res;
}
public void fun(int[] nums, int index){
res.add(new ArrayList<>(path));
for(int i = index; i < nums.length; i ++){
if(i != 0 && nums[i] == nums[i-1] && flag[i-1] == false){
continue;
}
flag[i] = true;
path.add(nums[i]);
fun(nums,i + 1);
flag[i] = false;
path.remove(path.size()-1);
}
}
}
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
boolean[] flag;
int count = 0;
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
flag = new boolean[candidates.length];
fun(candidates,target,0);
return res;
}
public void fun(int[] candidates, int target, int index){
if(count >= target){
if(count == target){
res.add(new ArrayList<>(path));
}
return ;
}
for(int i = index; i < candidates.length; i ++){
if(candidates[i] + count > target){
break;
}
if(i > 0 && candidates[i] == candidates[i-1] && flag[i-1] == false){
continue;
}
flag[i] = true;
path.add(candidates[i]);
count += candidates[i];
fun(candidates,target,i+1);
flag[i] = false;
path.remove(path.size()-1);
count -= candidates[i];
}
}
}
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
boolean[] flag;
public List<List<Integer>> permuteUnique(int[] nums) {
Arrays.sort(nums);
flag = new boolean[nums.length];
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] == false){
continue;
}
if(flag[i]){
continue;
}
flag[i] = true;
path.add(nums[i]);
fun(nums);
path.remove(path.size()-1);
flag[i] = false;
}
}
}
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
int count = 0;
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
fun(candidates,target,0);
return res;
}
public void fun(int[] candidates, int target, int index){
if(count >= target){
if(count == target){
res.add(new ArrayList<>(path));
}
return;
}
for(int i = index; i < candidates.length; i ++){
if(candidates[i] + count > target){
break;
}
path.add(candidates[i]);
count += candidates[i];
fun(candidates, target, i);
path.remove(path.size()-1);
count -= candidates[i];
}
}
}