[LeetCode] #40 Combination Sum II

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

Each number in C may only be used once in the combination.

Note:

  • All numbers (including target) will be positive integers.
  • Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
  • The solution set must not contain duplicate combinations.

For example, given candidate set 10,1,2,7,6,1,5 and target 8
A solution set is: 
[1, 7] 
[1, 2, 5] 
[2, 6] 
[1, 1, 6]

本题与上题类似,不过首先需要对数组进行排序,然后需要去除重复。时间:8ms。代码如下:

class Solution {
public:
    void makeCombination2(vector<vector<int>>& ret, vector<int>& candidates, vector<int>& v, int target, int cur){
        if (target == 0){
            ret.push_back(v);
            return;
        }
        int rep = -1;
        for (size_t i = cur; i < candidates.size(); ++i){
            if (rep == candidates[i])
                continue;
            if (target >= candidates[i]){
                rep = candidates[i];
                v.push_back(candidates[i]);
                makeCombination2(ret, candidates, v, target - candidates[i], i + 1);
                v.pop_back();
            }
            else
                return;
        }
    }
    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        vector<vector<int>> ret;
        sort(candidates.begin(), candidates.end());
        if (candidates.empty() || target < candidates[0])
            return ret;
        vector<int> v;
        makeCombination2(ret, candidates, v, target, 0);
        return ret;
    }
};
时间: 2024-08-09 22:01:54

[LeetCode] #40 Combination Sum II的相关文章

[array] leetcode - 40. Combination Sum II - Medium

leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combinat

leetCode 40.Combination Sum II(组合总和II) 解题思路和方法

Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including t

LeetCode 40. Combination Sum II (组合的和之二)

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be posi

leetcode 40 Combination Sum II --- java ----100%

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be posi

Java [Leetcode 40]Combination Sum II

题目描述: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will b

[LeetCode] 40. Combination Sum II Java

题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be

LeetCode 40 Combination Sum II(数组中求和等于target的所有组合)

题目链接:https://leetcode.com/problems/combination-sum-ii/?tab=Description 给定数组,数组中的元素均为正数,target也是正数.(数组中的元素可能有重复) 求出所有的满足求和等于terget的组合. 数组中的元素只能使用一次.(数组中重复的元素可以最多使用重复次数) 参考代码: package leetcode_50; import java.util.ArrayList; import java.util.Arrays; im

19.2.3 [LeetCode 40] Combination Sum II

Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target. Each number in candidates may only be used once in the combination. Note: All nu

[leetcode] 040. Combination Sum II (Medium) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 040. Combination Sum II (Medium) 链接: 题目:https://leetcode.com/problems/combination-sum-ii/ 代码(github):https://github.com/illuz/leetcode 题意: 跟 039 一样(给出一些正整数集合,