leetcode刷题(数组list回溯法)18—组合总和 II

40. 组合总和 II

给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的每个数字在每个组合中只能使用一次。
说明:
所有数字(包括目标数)都是正整数。
解集不能包含重复的组合。

class Solution {
    // 执行用时:20 ms, 在所有 Java 提交中击败了9.11%的用户
    // 内存消耗:39.9 MB, 在所有 Java 提交中击败了77.52%的用户
    //回溯法
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Deque<Integer> dp = new ArrayDeque<>();
        int len = candidates.length;
        List<List<Integer>> res = new ArrayList<>();
        DP(candidates,len , target, dp, res);
        List<List<Integer>> listNew=new ArrayList<List<Integer>>();
        for (List<Integer> list : res) {
            if(!listNew.contains(list)){
                listNew.add(list);
            }
        }
        return listNew;
    }
    public void DP(int[] candidates,int n, int target, Deque<Integer> dp,List<List<Integer>> res) {
        // //dp[n,k],当n=0时,返回当前数组中的数
        if(target == 0){
            List list = new ArrayList<>(dp);
            Collections.sort(list); 
            res.add(list);
             return;
        }
        if(n == 0){
             return;
        }
        int times = target / candidates[n - 1];
        times = Math.min(1,times);
        for(int i = 0;i <= times;++i){
            
            for(int j = 0; j < i; ++j){
                dp.addLast(candidates[n - 1]);
            }
            DP(candidates, n - 1,target - candidates[n - 1] * i, dp, res);
            for(int j = 0; j < i; ++j){
                dp.removeLast();
            }
        }
         return;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值