代码随想录Day027 | Leetcode 39.组合总和 、Leetcode 40.组合总和II、Leetcode 131.分割回文串

39.组合总和

class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        //必须先执行sort排序数组
        Arrays.sort(candidates);
        List<List<Integer>> res = new LinkedList<>();
        List<Integer> linkedList = new LinkedList<>();
        process(res, linkedList, candidates, 0, 0, target);

        return res;
    }

    public void process(List<List<Integer>> res, List<Integer> linkedList, int[] candidates,
                        int cur, int sum, int target) {

        if (sum == target) {
            //此时必须要写temp,不能直接把linkList放入res
            List<Integer> temp = new LinkedList<>(linkedList);
            res.add(temp);
            //linkedList = new LinkedList<>();无法置空,java递归调用的是原始引用,
            // 它不会更改调用方法中的引用或递归调用中的原始引用,用linkedList.clear();
//            linkedList = new LinkedList<>();
            return;
        }

        for (int i = cur; i < candidates.length && sum + candidates[i] <= target; i++) {
            sum += candidates[i];
            linkedList.add(candidates[i]);
            process(res, linkedList, candidates, i, sum, target);
            sum -= candidates[i];
            linkedList.remove(linkedList.size() - 1);

        }

    }


}

40.组合总和II

class Solution {
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        List<List<Integer>> res = new LinkedList<>();
        List<Integer> linkedList = new LinkedList<>();
        boolean[] usd = new boolean[candidates.length];
        //一定要先排序
        Arrays.sort(candidates);
        process(res, linkedList, usd, 0, 0, candidates, target);
        return res;
    }

    public void process(List<List<Integer>> res, List<Integer> linkedList, boolean[] used, int cur, int sum, int[] candidates, int target) {
        if (sum == target) {
            List<Integer> temp = new LinkedList<>(linkedList);
            res.add(temp);

            return;
        }


        for (int i = cur; i < candidates.length && sum + candidates[i] <= target; i++) {
            //核心代码
            if (i > 0 && !used[i - 1] && candidates[i] == candidates[i - 1]) {
                continue;
            }


            linkedList.add(candidates[i]);
            sum += candidates[i];
            used[i] = true;
            process(res, linkedList, used, i + 1, sum, candidates, target);
            sum -= candidates[i];
            linkedList.remove(linkedList.size() - 1);
            used[i] = false;

        }


    }
}

131.分割回文串


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;
    }
}
  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值