力扣labuladong——一刷day07

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • 一、力扣78. 子集
  • 二、力扣77. 组合
  • 三、力扣46. 全排列
  • 四、力扣90. 子集 II
  • 五、力扣40. 组合总和 II
  • 六、力扣47. 全排列 II
  • 七、力扣39. 组合总和


前言


一、力扣78. 子集

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);
        }
    }
}

二、力扣77. 组合

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);
        }
    }
}

三、力扣46. 全排列

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;
            }
        }
    }
}

四、力扣90. 子集 II

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);
        }
    }
}

五、力扣40. 组合总和 II

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];
        }
    }
}

六、力扣47. 全排列 II

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;
        }
    }
}

七、力扣39. 组合总和

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];
        }
    }
}

你可能感兴趣的:(leetcode,算法,职场和发展,java)