美文网首页快乐写代码
T40、数组总和II

T40、数组总和II

作者: 上行彩虹人 | 来源:发表于2020-05-25 23:12 被阅读0次

给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的每个数字在每个组合中只能使用一次。
说明:
所有数字(包括目标数)都是正整数。
解集不能包含重复的组合。
示例 1:
输入: candidates = [10,1,2,7,6,1,5], target = 8,
所求解集为:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]
示例 2:
输入: candidates = [2,5,2,1,2], target = 5,
所求解集为:
[
[1,2,2],
[5]
]

需要对结果去重,所以最重要的一步是对数组排序

  List<List<Integer>> res;
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        res = new LinkedList<>();
        Arrays.sort(candidates);
        find(candidates,0,new LinkedList(),target);
        return res;
    }

    public void find(int[] candidates,int idx,LinkedList<Integer> temp, int need){
        if(need==0 && !res.contains(temp)){
            res.add(new LinkedList<>(temp));
            return;
        }else if(need < 0)
            return;
       
        for(int i = idx; i < candidates.length; i++){
            temp.add(candidates[i]);
            need -= candidates[i];
            find(candidates,i+1,temp,need);
            // temp.remove(temp.removeLast()-1);
            temp.removeLast();
            need += candidates[i];
        }
    }

解2

 List<List<Integer>> res;
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        res = new LinkedList<>();
        Arrays.sort(candidates);
        find(candidates,0,new LinkedList(),target);
        return res;
    }

    public void find(int[] candidates,int idx,LinkedList<Integer> temp, int need){
        if(need==0){
            res.add(new LinkedList<>(temp));
            return;
        }else if(need < 0)
            return;
       
        for(int i = idx; i < candidates.length; i++){
            // 判断当前元素是否已经被使用过了
            if(i > idx && candidates[i] == candidates[i-1])
                continue;
            temp.add(candidates[i]);
            need -= candidates[i];
            find(candidates,i+1,temp,need);
            // temp.remove(temp.removeLast()-1);
            temp.removeLast();
            need += candidates[i];
        }
    }

相关文章

  • T40、数组总和II

    给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为...

  • 数组相同数字聚集

    题目描述: 昨天做 leetcode 组合总和 II 这个题目时,碰到这样一个需求:给定一个数组,数组中存在相同的...

  • LeetCode-040-组合总和 II

    组合总和 II 题目描述:给定一个数组 candidates 和一个目标数 target ,找出 candidat...

  • 40. 组合总和 II

    题目 40. 组合总和 II 给定一个数组 candidates 和一个目标数 target ,找出 candid...

  • LeetCode | 0113. Path Sum II路径总和

    LeetCode 0113. Path Sum II路径总和 II【Medium】【Python】【回溯】 Pro...

  • 路径总和II

    给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径。 说明: 叶子节点是指没有子节...

  • 40. 组合总和 II

    40. 组合总和 II(难度:中等) 题目链接:https://leetcode-cn.com/problems/...

  • leecode刷题(6)-- 两个数组的交集II

    leecode刷题(6)-- 两个数组的交集II 两个数组的交集II 描述: 给定两个数组,编写一个函数来计算它们...

  • 40 组合总和 II

    题目: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使...

  • 算法分析 [n值加法、平方加法] 2019-03-07

    1. n值加法 167. 两数之和 II - 输入有序数组 Two Sum II - Input array is...

网友评论

    本文标题:T40、数组总和II

    本文链接:https://www.haomeiwen.com/subject/udlpahtx.html