• 代码随想录二刷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
  • 相关阅读:
    Unity 关节:铰链、弹簧、固定、物理材质:摩檫力、 特效:拖尾、
    MySql生成ER【StarUML】文件
    做外贸必知——你还不知道外贸拼箱技巧吗
    Kubernetes(k8s)是什么?解决了哪些问题?
    磁盘管理 及 nfs服务配置
    DP4301芯片简介
    你说,网络出口选择防火墙多,还是路由器多?
    【重拾C语言】六、批量数据组织(二)线性表——分类与检索(主元排序、冒泡排序、插入排序、顺序检索、对半检索)
    【日志系统】Loki日志监控 - 入门初体验
    【PG】PostgreSQL字符集
  • 原文地址:https://blog.csdn.net/ResNet156/article/details/132848796