Combination Sum II

Source: Internet
Author: User

https://leetcode.com/problems/combination-sum-ii/

The topic is similar to the previous questions, write the code directly:

classSolution { Public: Vector<vector<int>> combinationSum2 (vector<int>& candidates,inttarget)        {Sort (Candidates.begin (), Candidates.end ()); if(candidates.size () = =0)            returnRes; Vector<int>temp; Helper (candidates,0, target,temp); returnRes; }Private:    voidHelper (vector<int>& candidates,intIndexinttarget,vector<int>&temp) {        if(target==0) Res.push_back (temp); if(target<0|| index>=candidates.size ()) {            return; }                     for(intI=index;i<candidates.size (); i++)        {            if(I>index && candidates[i]==candidates[i-1])//You must ensure that the first position cannot be repeated when a value is passed down! But the first time it must be preserved, so it must be written i>index, not i>0!!. 1,1,2,5,6,7,10 8 "1,2,5" "1,2,5" "1,1,6" for this kind of situation                Continue;            Temp.push_back (Candidates[i]); Helper (Candidates,i+1, target-candidates[i],temp);//Note that this place should not be index, but should pass in i+1, otherwise, will appear descending sequenceTemp.pop_back (); }    }Private: Vector<vector<int>>Res;};

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.