[Leetcode] Combination Sum II

Source: Internet
Author: User

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

Problem Solving Ideas:

Similar to combination sum, the difference is that each candidate element can only be used once.

Class Solution {public:vector<vector<int>> combinationSum2 (vector<int>& candidates, int target        ) {vector<vector<int>> result;        int len=candidates.size ();        if (len==0) {return result;        } std:sort (Candidates.begin (), Candidates.end ());  Map<int, int> Keytonumber;        The equivalent coefficient, which indicates how many times each number appears, notice that it becomes subscript, not number.  Set<vector<int>> contains;    Whether it contains this set of solutions GetResult (result, contains, candidates, 0, Keytonumber, target); } void GetResult (vector<vector<int>>& result, set<vector<int>>& contains, vector&lt             ;int>& uniquecandidates, int candidateindex, map<int, int>& keytonumber, int left) {if (left<0) {        Return            } if (left==0) {vector<int> item; For (Map<int, Int>::iterator it=keytonumber.begin (); It!=keytonumber.end (); it++) {int number=it->s         Econd;       int key = it->first;                for (int i=0; i<number; i++) {item.push_back (Uniquecandidates[key]);                }} if (Contains.find (item) ==contains.end ()) {Result.push_back (item);            Contains.insert (item);        } return;        } if (Candidateindex>=uniquecandidates.size ()) {return;        } int number=0; while (LEFT&GT;=0&AMP;&AMP;NUMBER&LT;2) {//used 0 times or once if (number!=0) keytonumber[candidateindex]=num            ber            GetResult (result, contains, uniquecandidates, Candidateindex+1, Keytonumber, left);            if (number!=0) {keytonumber.erase (candidateindex);            } left = Left-uniquecandidates[candidateindex];        number++; }    }};


[Leetcode] 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.