Leetcode 39. 组合总和 40. 组合总和 II 131. 分割回文串

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


39. 组合总和


● 今日学习的Leetcode链接

组合总和

● 自己看到题目的第一想法

和前边的题类似

● 看完代码随想录之后的想法

剪枝

  1. 在for循环中,如果candidates排好序,那如果已经大于sum时,就可以不用遍历后边的了,直接break

● 自己实现过程中遇到哪些困难

没有剪枝

● 今日收获,记录一下自己的学习时长

很大

class Solution {

    LinkedList<Integer> list = new LinkedList<>();
    List<List<Integer>> result = new ArrayList<>();
    int sum = 0;

    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        combination(candidates,target,0);
        return result;
    }

    public void combination(int[] candidates, int n, int start){
        if(sum > n){
            return;
        }
        if(sum == n){
           result.add(new ArrayList<>(list));
           return;
        }

        for(int i = start;i< candidates.length ;i++){
            if(sum + candidates[i] > n){ //要在排好序的前提下,才能剪枝
                continue;
            }
            list.add(candidates[i]);
            sum += candidates[i];
            combination(candidates,n,i);
            sum -= candidates[i];
            list.removeLast();
        }
    }
}

40. 组合总和 II


● 今日学习的Leetcode链接

组合总和 II

● 自己看到题目的第一想法

需要去重,用map 可能会超时

● 看完代码随想录之后的想法

去重操作

  1. 要树层去重,不用树枝去重 即要横向去重,不要纵向去重
  2. 例: 1 2 2 3 可以 1 2 2,但是不可以重复 1 2(1) 3 和 1 2(2) 3

● 自己实现过程中遇到哪些困难

  1. 要有排序的前提
  2. 要多加一个used数组,记录有没有使用过,要是本次数和上一次相同且上一个数没有用过,那就continue跳过

● 今日收获,记录一下自己的学习时长

很大

class Solution {

    LinkedList<Integer> list = new LinkedList<>();
    List<List<Integer>> result = new ArrayList<>();
    int sum = 0;
    boolean[] used;

    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        used = new boolean[candidates.length];
        Arrays.fill(used,false);//填充false
        Arrays.sort(candidates);//排序
        combination(candidates,target,0);
        return result;
    }

     public void combination(int[] candidates, int n, int start){
        if(sum > n){
            return;
        }
        if(sum == n){
           result.add(new ArrayList<>(list));
           return;
        }

        for(int i = start;i<candidates.length;i++){
            if (sum + candidates[i] > n) { //剪枝
                break;
            }
            if (i>0 && candidates[i]==candidates[i-1] && used[i-1]==false){ 
                continue;
            }
            list.add(candidates[i]);
            sum += candidates[i];
            used[i] = true;
            combination(candidates,n,i+1);
            sum -= candidates[i];
            list.removeLast();
            used[i] = false;
        }
    }
}

131. 分割回文串


● 今日学习的Leetcode链接

分割回文串

● 自己看到题目的第一想法

很难

● 看完代码随想录之后的想法

  1. 如何实现判断回文串
  2. 切分范围的划定? 从startIndex开始,包括startIndex,到i为止,包括i
  3. 切割后两边都应该判断是不是回文串,但是其他的不用,因为前边的已经判断过了。切割后的一部分也不用判断,因为有可能后半部分没有切割之前不是回文,切割后就是回文了,后边的部分留在后边解决。
  4. 怎样做切割,“a” “aa” ? 直接在原串上比对,传入开始和结尾下标,分开时用substring()做切分。是List集合,所以在如果是回文,就放进去,不是的话回溯删除list结尾就好。最终结果是放在result中的
  5. substring() 不包括末端

● 自己实现过程中遇到哪些困难

太多了

● 今日收获,记录一下自己的学习时长

很长

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、付费专栏及课程。

余额充值