[leetcode] Combination Sum and Combination SumII

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

The same repeated number may be chosen from C unlimited number of times.

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 2,3,6,7 and target 7,

A solution set is:

[7]

[2, 2, 3]

class Solution {
private:
    vector<vector<int> > ivvec;
public:
    vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
        vector<int> ivec;
        sort(candidates.begin(), candidates.end());
        combinationSum(candidates, 0, target, ivec);
        return ivvec;
    }

    void combinationSum(vector<int> &candidates, int beg, int target, vector<int> ivec)
    {
        if (0 == target)
        {
            ivvec.push_back(ivec);
            return;
        }
        for (int idx = beg; idx < candidates.size(); ++idx)
        {
            if (target - candidates[idx] < 0)
                break;
            ivec.push_back(candidates[idx]);
            combinationSum(candidates, idx, target - candidates[idx], ivec);
            ivec.pop_back();
        }
    }
};

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]

与上题差别不大。依然是用DFS,基本的问题在于怎样去重。

能够添加一个剪枝: 当当前元素跟前一个元素是同样的时候。假设前一个元素被取了,那当前元素能够被取,也能够不取,反过来假设前一个元素没有取。那我们这个以及之后的所以同样元素都不能被取。

(採用flag的向量作为标记元素是否被选取)

class Solution {
private:
    vector<vector<int> > ivvec;
    vector<bool> flag;
public:
    vector<vector<int> > combinationSum2(vector<int> &num, int target) {
        vector<int> ivec;
        sort(num.begin(), num.end());
        flag.resize(num.size());
        for (int i = 0; i < flag.size(); ++i)
            flag[i] = false;
        combinationSum2(num, 0, ivec, target, 0);
        return ivvec;
    }

    void combinationSum2(vector<int> &num, int beg, vector<int> ivec, int target, int sum)
    {
        if (sum > target)
            return;
        if (sum == target)
        {
            ivvec.push_back(ivec);
            return;
        }

        for (int idx = beg; idx < num.size(); ++idx)
        {
            if (sum + num[idx] > target) continue;
            if (idx != 0 && num[idx] == num[idx - 1] && flag[idx - 1] == false)
                continue;
            flag[idx] = true;
            ivec.push_back(num[idx]);
            combinationSum2(num, idx + 1, ivec, target, sum + num[idx]);
            ivec.pop_back();
            flag[idx] = false;
        }
    }
};

版权声明:本文博主原创文章。博客,未经同意不得转载。

时间: 2024-10-12 07:24:36

[leetcode] Combination Sum and Combination SumII的相关文章

Combination Sum 和Combination Sum II

这两道题的基本思路和combination那一题是一致的,也是分治的方法. 其中combination Sum复杂一点,因为每个数可能用多次.仔细分析下,本质上也是一样的.原来是每个数仅两种可能.现在每个数有k +1中可能,k = target / i. 所以就是把简单的if else 分支变成for循环: vector<vector<int> > combHelper(vector<int>::iterator first, vector<int>::it

Combination Sum,Combination Sum II,Combination Sum III

39. Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (inc

leetcode笔记:Combination Sum III

一. 题目描述 Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. E

[leetcode]Combination Sum @ Python

原题地址:https://oj.leetcode.com/problems/combination-sum/ 题意: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited

LeetCode Combination Sum III

原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 与Combination Sum II相似,不同的是中不是所有元素相加,只是k个元素相加. 所以在把item的copy 加到res前需要同时满足item.size() == k 和 target == 0两个条件. AC Java: 1 public class Solution { 2 public List<List<Integer>> combinationS

Java for LeetCode 216 Combination Sum III

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. Example 1

[LeetCode][JavaScript]Combination Sum III

Combination Sum III Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascend

[Leetcode][Python]40: Combination Sum II

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 40: Combination Sum IIhttps://oj.leetcode.com/problems/combination-sum-ii/ Given a collection of candidate numbers (C) and a target number (T),find all unique combinations in C where the candi

LeetCode: Combination Sum [038]

[题目] Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target)