• 代码随想录二刷day27


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


    前言


    一、力扣39. 组合总和

    class Solution {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        public List<List<Integer>> combinationSum(int[] candidates, int target) {
            fun(candidates, target, 0, 0);
            return res;
        }
        public void fun(int[] candidates, int target, int count, int start){
            if(count >= target){
                if(count == target){
                    res.add(new ArrayList<>(path));
                }
                return ;
            }
            for(int i = start; i < candidates.length; i ++){
                count += candidates[i];
                path.add(candidates[i]);
                fun(candidates, target, count, i);
                count -= candidates[i];
                path.remove(path.size() - 1);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    二、力扣40. 组合总和 II

    class Solution {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        boolean[] flag;
        public List<List<Integer>> combinationSum2(int[] candidates, int target) {
            Arrays.sort(candidates);
            flag = new boolean[candidates.length];
            Arrays.fill(flag, false);
            fun(candidates, 0, target, 0);
            return res;
        }
        public void fun(int[] candidates, int start, int target, int count){
            if(count >= target){
                if(target == count){
                    res.add(new ArrayList(path));
                } 
                return ;
            }
            for(int i = start; i < candidates.length; i ++){
                if(candidates[i] + count > target){
                    break;
                }
                if(i > 0 && candidates[i-1] == candidates[i] && flag[i-1] == false){
                    continue;
                }
                count += candidates[i];
                path.add(candidates[i]);
                flag[i] = true;
                fun(candidates, i+1, target, count);
                count -= candidates[i];
                path.remove(path.size()-1);
                flag[i] = false;
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    无标记数组

    class Solution {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        public List<List<Integer>> combinationSum2(int[] candidates, int target) {
            Arrays.sort(candidates);
            fun(candidates, 0, target, 0);
            return res;
        }
        public void fun(int[] candidates, int start, int target, int count){
            if(count >= target){
                if(target == count){
                    res.add(new ArrayList(path));
                } 
                return ;
            }
            for(int i = start; i < candidates.length; i ++){
                if(candidates[i] + count > target){
                    break;
                }
                if(i > start && candidates[i-1] == candidates[i]){
                    continue;
                }
                count += candidates[i];
                path.add(candidates[i]);
                fun(candidates, i+1, target, count);
                count -= candidates[i];
                path.remove(path.size()-1);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    三、力扣131. 分割回文串

    class Solution {
        List<List<String>> res = new ArrayList<>();
        List<String> path = new ArrayList<>();
        public List<List<String>> partition(String s) {
            fun(s,0);
            return res;
        }
        public void fun(String s, int start){
            if(start >= s.length()){
                res.add(new ArrayList(path));
                return ;
            }
            for(int i = start; i < s.length(); i ++){
                if(flag(s, start, i)){
                    String str = s.substring(start, i +1);
                    path.add(str);
                    fun(s, i + 1);
                }else{
                    continue;
                }
                path.remove(path.size()-1);
            }
        }
        public boolean flag(String s, int low, int high){
            for(int i = low, j = high; i <= j; i ++, j --){
                if(s.charAt(i) != s.charAt(j)){
                    return false;
                }
            }
            return true;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
  • 相关阅读:
    WeNet:面向工业落地的E2E语音识别工具
    mysql 指定多个IP 绑定监听地址 bind_address
    5G DTU终端
    电磁场与电磁波part1--矢量分析
    SAP MM学习笔记34 - 请求书照合中的支付保留(发票冻结)
    Linux内核CPU调度域内容讲解
    Docker入门——安装部署(openEuler)、镜像加速
    11.22IG客户情绪报告: 黄金、原油、澳元、日元、欧元、英镑
    阿里云SLB负载均衡产品基本概念与购买流程
    oracle分组排序去重
  • 原文地址:https://blog.csdn.net/ResNet156/article/details/132848796