代码随想录二刷day27

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

文章目录

  • 前言
  • 一、力扣39. 组合总和
  • 二、力扣40. 组合总和 II
  • 三、力扣131. 分割回文串


前言


一、力扣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);
        }
    }
}

二、力扣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;
        }
    }
}

无标记数组

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

三、力扣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;
    }
}

你可能感兴趣的:(leetcode,算法,数据结构,java)