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;
    }
}
相关推荐
!停9 分钟前
C语言单链表
c语言·数据结构·算法
独自破碎E11 分钟前
【队列】求二叉树的层序遍历
leetcode
闻缺陷则喜何志丹19 分钟前
【回文 字符串】3677 统计二进制回文数字的数目|2223
c++·算法·字符串·力扣·回文
Tisfy26 分钟前
LeetCode 0085.最大矩形:单调栈
算法·leetcode·题解·单调栈
mit6.82427 分钟前
出入度|bfs|状压dp
算法
hweiyu0028 分钟前
强连通分量算法:Kosaraju算法
算法·深度优先
源代码•宸28 分钟前
Golang语法进阶(定时器)
开发语言·经验分享·后端·算法·golang·timer·ticker
mit6.82435 分钟前
逆向思维|memo
算法
机器学习之心36 分钟前
MATLAB灰狼优化算法(GWO)改进物理信息神经网络(PINN)光伏功率预测
神经网络·算法·matlab·物理信息神经网络
代码游侠40 分钟前
学习笔记——ESP8266 WiFi模块
服务器·c语言·开发语言·数据结构·算法