刷题训练day23 | 第七章 回溯算法 part03

题目1:【二刷完成】

class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        backtracking(candidates,target,0,0);
        return result;

    }
    public void backtracking(int[] candidates, int target, int sum, int startIndex){
        if (sum>target) return;
        if (sum==target){
            result.add(new ArrayList<>(path));
            return;
        }
        for (int i=startIndex; i<candidates.length;i++){
            path.add(candidates[i]);
            sum += candidates[i];
            backtracking(candidates,target,sum,i);
            sum -= candidates[i];
            path.remove(path.size()-1);
        }
    }
}

题目2:【二刷完成】

class Solution {
    List<Integer> path = new ArrayList<>();
    List<List<Integer>> result = new ArrayList<>();
    boolean[] used;
    //int sum = 0;
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        used = new boolean[candidates.length];
        Arrays.sort(candidates);
         // 加标志数组,用来辅助判断同层节点是否已经遍历
         Arrays.fill(used, false);
         // 为了将重复的数字都放到一起,所以先进行排序
        backtracking(candidates, target, 0,0);
        return result;
    }
    public void backtracking(int[] candidates, int target, int startIndex, int sum ){
       
        if (sum==target){
            result.add(new ArrayList<>(path));
        }
        for (int i=startIndex; i<candidates.length;i++){
            if (sum+candidates[i]>target){
                break;
            }
            // 出现重复节点,同层的第一个节点已经被访问过,所以直接跳过
             if (i > 0 && candidates[i] == candidates[i - 1] && !used[i - 1]) {
                 continue;
            }
            used[i]=true; 
            path.add(candidates[i]);
            sum += candidates[i];
            backtracking(candidates,target,i+1,sum);
            used[i]=false;
            sum -= candidates[i];
            path.remove(path.size()-1);
        }
    }
}

题目3:【二刷完成】

class Solution {
    List<List<String>> lists = new ArrayList<>();
    Deque<String> deque = new LinkedList<>();

    public List<List<String>> partition(String s) {
        backTracking(s, 0);
        return lists;
    }

    private void backTracking(String s, int startIndex) {
        //如果起始位置大于s的大小,说明找到了一组分割方案
        if (startIndex >= s.length()) {
            lists.add(new ArrayList(deque));
            return;
        }
        for (int i = startIndex; i < s.length(); i++) {
            //如果是回文子串,则记录
            if (isPalindrome(s, startIndex, i)) {
                String str = s.substring(startIndex, i + 1);
                deque.addLast(str);
            } else {
                continue;
            }
            //起始位置后移,保证不重复
            backTracking(s, i + 1);
            deque.removeLast();
        }
    }
    //判断是否是回文串
    private boolean isPalindrome(String s, int startIndex, int end) {
        for (int i = startIndex, j = end; i < j; i++, j--) {
            if (s.charAt(i) != s.charAt(j)) {
                return false;
            }
        }
        return true;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值