Leetcode:combination Sum II

Source: Internet
Author: User

Combination Sum II
Total Accepted: 71124 Total Submissions: 254398 Difficulty: Medium

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.
    • The solution set must not contain duplicate combinations.

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

[  [1, 7],  [1, 2, 5],  [2, 6],  [1, 1, 6]

Subscribe to see which companies asked this question

Hide TagsArray BacktrackingHide Similar Problems(M) combination Sum






























C + + code:

Class Solution {public:vector<vector<int>> combinationSum2 (vector<int>& candidates, int target        ) {vector<vector<int>> combs;        Vector<int> comb; Sort (Candidates.begin (), Candidates.end ());        Sort or candidates the number of combinationSum2 (Combs,comb,candidates,0,target);    return combs; }//Custom function void combinationSum2 (vector<vector<int>>& combs, vector<int>& comb, vector&        lt;int>& candidates, int start, int target) {if (Target < 0) return;            else if (target==0) {combs.push_back (comb);        Return  } for (int i=start;i<candidates.size (); i++) {if (I>start && candidates[i-1]==candidates[i])            Continue            Comb.push_back (Candidates[i]);            CombinationSum2 (Combs,comb,candidates,i+1,target-candidates[i]);        Comb.pop_back (); }    }};


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.