Combination Sum II

Source: Internet
Author: User

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 is used once in the combination.

Note:

  • All numbers (including target) would be positive integers.

  • Elements in a combination (a1, a2, ..., aK) must is in non-descending Order. (ie, a1 ≤ a2 ≤ ... ≤ ak).

  • 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]

Solution:

Note that this problem is described in collection C, and the examples given are repeating elements; Test instructions requires that each element be used only once, then backtracking,

The next time we should start from i+1,

In order to remove the weight (the array is already sorted),

void Combinationcore (vector<int> &candi,int target,int begin,vector<int> &tempresult,vector< Vector<int>> &results) {

if (target==0)

Results.push_back (Tempresult);

else{

int size=candi.size ();

for (int i=begin;i<size&&target>=candi[i];++i) {

if (i==begin| | Candi[i]!=candi[i-1]) {

Here i==begin| | Candi[i]!=candi[i-1], it should be noted, because each time there is a i==begin, then each recursive, regardless of whether this element and the previous element is equal will continue to combinationcore, this can guarantee that if there is a continuous 2 2 2 in the 6 o'clock can be established,

But once the second for loop, it depends on whether it is equal to the previous one, and if it is equal, it is not handled, because if the subsequent solution is obtained, it must have been obtained for the first for loop. The repetition will be repeated.

Tempresult.push_back (Candi[i]);

Combinationcore (Candi,target-candi[i],i+1, tempresult,results); //This ensures that each element is only taken once, because the next time it starts from I+1,

Tempresult.pop_back ();

}

}

}

}

Vector<vector<int>> combinationSum2 (vector<int>& candidates, int target) {

int size=candidates.size ();

vector<vector<int>> results;

Vector<int> temp;

if (size==0| | target<=0)

return results;

Sort (Candidates.begin (), Candidates.end ());

Combinationcore (Candidates,target,0,temp,results);

return results;

}



Combination Sum II

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.