[Leetcode] Combination Sum and Combination sumii

Source: Internet
Author: User

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 is chosen from C unlimited number of times.

Note:

  • All numbers (including target) would be positive integers.
  • elements in a combination (a 1 ,  a 2 , ...,  a k ) must is in non-descending order. (Ie, a 1  ≤ a 2  ≤ ... ≤ a k ).
  • The solution set must not contain duplicate combinations.

For example, given candidate set2,3,6,7and target7,
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 = = Ta Rget)        {            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 inCwhere the candidate numbers sums toT.

Each number in C is used once in the combination.

Note:

  • All numbers (including target) would be positive integers.
  • elements in a combination (a 1 ,  a 2 , ...,  a k ) must is in non-descending order. (Ie, a 1  ≤ a 2  ≤ ... ≤ a k ).
  • The solution set must not contain duplicate combinations.

For example, given candidate set10,1,2,7,6,1,5and target8,
A Solution set is:
[1, 7]
[1, 2, 5]
[2, 6]
[1, 1, 6]

There is little difference with the above question. Still using DFS, the basic question is how to weigh.

Ability to add a pruning: when the current element is the same as the previous element. Assuming that the previous element is taken, the current element can be taken and not taken, which in turn assumes that the previous element was not taken. So the same element cannot be taken from us or later.

(The vector using flag is selected as the marker element)

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) Conti            Nue            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; }    }};


Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

[Leetcode] Combination Sum and Combination sumii

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.