0
点赞
收藏
分享

微信扫一扫

LeetCode第39题 组合总和

艾晓雪 2022-01-23 阅读 57

在这里插入图片描述

第一反应

递归,比如示例1,[2,3,6,7] target=7,我的想法是依次从target中减去一个待选数(比如7-2=5),然后对得到的数字再递归调用本体函数。
如果要优化的话,可以考虑对于得到的结果存进数组,以避免反复调用某个target浪费。

看看题解

当做一个树状图的搜索。每一个选择分支都有选择这个数字、不选这个数字两种,如果不选这个数字就把指针移动到下一个数字

看完题解再想

是不是,比如说树状图的搜索,如果在比如左侧的第一个分支表示选择2,在右侧的其他分支就不用考虑选择2的情况了?

复习java

遍历list

        for(String value:list){
            System.out.println(value);
        }

提交通过

我感觉做的还挺漂亮的

class Solution {
    public int[] candidates;
    public int len;
    public List<List<Integer>> ans = new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        Arrays.sort(candidates);
        this.candidates = candidates;
        this.len = candidates.length;
        dfs(target, new ArrayList<>(), candidates[0]);
        return ans;
    }
    /*
    candidates 是作为不变的公共输入信息
    target 在不同的节点可能有不同的数值
    head 为当前节点所包含的头部数字串,这个节点的子节点所导出的结果必定包含这个head
    bound 为边界,candidate中小于bound的数字不会被选择(以防止重复)
    */
    public void dfs(int target, List<Integer> head_org, int bound){
        List<Integer> head = copyList(head_org);
        for(int i = 0 ; i < len ; i++){
            if(candidates[i] < bound){
                continue;
            }
            if(candidates[i] < target){
                List<Integer> head_temp = copyList(head);
                head_temp.add(candidates[i]);
                dfs(target - candidates[i] , head_temp, candidates[i]);
            }else if(candidates[i] == target){
                head.add(candidates[i]);
                ans.add(head);
                return;
            }else{
                return;
            }
        }
    }
    public List<Integer> copyList(List<Integer> list1){
        List<Integer> newList = new ArrayList<>();
        for(int value:list1){
            newList.add(value);
        }
        return newList;
    }
}
举报

相关推荐

0 条评论