Leetcode brushing questions (array list backtracking method) 18-combined sum II

40. Portfolio Sum II

Given an array of candidates and a target number target, find out all the combinations in candidates that can make the sum of numbers into target.
Each number in candidates can only be used once in each combination.
Explanation:
All numbers (including the target number) are positive integers.
A solution set cannot contain duplicate combinations.

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;
    }
}

Guess you like

Origin blog.csdn.net/qq_38754625/article/details/108521783