力扣labuladong——一刷day07

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

文章目录

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

前言


一、力扣78. 子集

java 复制代码
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. 组合

java 复制代码
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. 全排列

java 复制代码
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

java 复制代码
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

java 复制代码
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

java 复制代码
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. 组合总和

java 复制代码
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];
        }
    }
}
相关推荐
一去不复返的通信er4 小时前
力扣-图论-岛屿数量
算法·leetcode·图论
考研保研资料分享5 小时前
985工科跨保上财应用统计:数学笔试、匿名面试与机试复盘
面试·职场和发展
wangchunyu1146 小时前
JAVA CompletableFuture异步编程实战
java·开发语言·python
paopaokaka_luck7 小时前
流浪动物救助与收养系统(DeepSeek AI对话、 ECharts多维分析、救助流程衔接、领养与回访衔接、 捐赠确认与公示、志愿者协作)
java·前端·javascript·人工智能·mysql·echarts·推荐算法
Thomas.Sir7 小时前
第9课:SpringBoot4.x Web开发基础:WebMvc自动配置、静态资源、请求参数接收
java·spring boot
程序 代码狂人7 小时前
切片表与分区表的区别(数据开发面试版)
java·大数据·开发语言·数据仓库·算法
HDU-Dade7 小时前
【校招笔试真题】简要介绍 Self-Attention 的前沿算法
人工智能·算法
2501_944521598 小时前
Deepmerge 跨端三方库仓颉适配全流程
java·python·eclipse·github
Xin7708 小时前
LeetCode 79. 单词搜索(DFS 回溯法)
leetcode
南归北隐9 小时前
Spring AI Alibaba Graph框架实现MCP数据库查询实战
java·后端·spring ai·mcp·spring ai mcp