23th Day| 39.组合总和,40.组合总和II,131.分割回文串

LeetCode 39 组合总和

题目链接:39.组合总和

java 复制代码
//剪枝优化
class Solution {
    List<Integer> path = new ArrayList<>();
    List<List<Integer>> res = new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        Arrays.sort(candidates);
        backtracking(candidates, target,0, 0);
        return res;
    }

    public void backtracking(int[] nums, int target, int sum, int startIndex){
        if(sum == target){
            res.add(new ArrayList(path));
            return;
        }
        for(int i = startIndex; i < nums.length; i++){
            if(sum+nums[i] > target) break;
            path.add(nums[i]);
            backtracking(nums, target,sum+nums[i], i);//取得是当前元素
            path.removeLast();
        }
    }
}

LeetCode 40 组合总和II

题目链接:40.组合总和II

java 复制代码
class Solution {
    List<Integer> path = new ArrayList<>();
    List<List<Integer>> res = new ArrayList<>();
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Arrays.sort(candidates);
        backtracking(candidates, target, 0, 0);
        return res;
    }

    public void backtracking(int[] nums, int target, int sum, int startIndex){
        if(sum == target){
            res.add(new ArrayList(path));
            return;
        }
        for(int i = startIndex; i < nums.length; i++){
            if(sum+nums[i] > target) break;
            if(i != startIndex && nums[i-1] == nums[i]) continue;//i > startIndex
            path.add(nums[i]);
            backtracking(nums, target, sum+nums[i], i+1);
            path.removeLast();
        }
    }
}

LeetCode 131 分割回文串

题目链接:131.分割回文串

java 复制代码
class Solution {
    LinkedList<String> path = new LinkedList<>();
    List<List<String>> res = new ArrayList<>();
    public List<List<String>> partition(String s) {
        backtracking(s, 0 , new StringBuilder());
        return res;
    }
    //每一个for都需要一个新的StringBuilder;
    public void backtracking(String s, int startIndex, StringBuilder sb){
        if(startIndex == s.length()){
            res.add(new ArrayList(path));
            return;
        }
        for(int i = startIndex; i < s.length(); i++){
            sb.append(s.charAt(i));
            if(check(sb)){
                path.add(sb.toString());
                backtracking(s, i+1, new StringBuilder());
                path.removeLast();
            }else{
                continue;
            }
        }
    }

    public boolean check(StringBuilder sb){
        int i = 0;
        int j = sb.length() -1;
        while(i < j){
            if(sb.charAt(i++) != sb.charAt(j--)) return false;
        }
        return true;
    }
}
相关推荐
_dindong15 小时前
牛客101:递归/回溯
数据结构·c++·笔记·学习·算法·leetcode·深度优先
刃神太酷啦16 小时前
力扣校招算法通关:双指针技巧全场景拆解 —— 从数组操作到环检测的高效解题范式
java·c语言·数据结构·c++·算法·leetcode·职场和发展
西瓜树枝16 小时前
遗传算法与属性约简:原理、代码与参数配置
算法
天才测试猿16 小时前
Postman使用方法
自动化测试·软件测试·测试工具·职场和发展·测试用例·接口测试·postman
jerryinwuhan16 小时前
理论及算法_时间抽取论文
前端·算法·easyui
小蒋学算法16 小时前
贪心算法:IPO
算法
得物技术17 小时前
大模型如何革新搜索相关性?智能升级让搜索更“懂你”|得物技术
算法·搜索引擎·排序算法
mit6.82417 小时前
hash滑窗|dp
算法
Shinom1ya_17 小时前
算法 day 42
数据结构·算法·leetcode