Combination Sum II

Source: Internet
Author: User

1. Title

Combination Sum II

2. Http Address

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

3. The question

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, a 2, ..., aK) must is in non-descending order. (ie, a1≤ a2≤ ... ≤ ak).
    • 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]

4. My Code (AC)

1     //Accepted2         PublicList<list<integer>> combinationSum2 (int[] candidates,inttarget) {3 4list<list<integer>> result =NewArraylist<list<integer>>();5             if(Candidates = =NULL)6                 returnresult;7             8             intLen =candidates.length;9             intsum = 0;Tenlist<integer> sublist =NewArraylist<integer>(); One Arrays.sort (candidates); A              for(inti = 0; i < Len; i++) -             { -sum =Candidates[i]; the Sublist.add (Candidates[i]); -Getcombinationsum (candidates, i + 1, Sum, target,sublist, result); -sum = 0; -Sublist.remove (0); +             } -             returnresult;  +           } A         Private voidGetcombinationsum (int[] candidates,intBeginintSum, at                 intTarget, list<integer> sublist, list<list<integer>>result) { -  -             if(Sum >target) -             { -                 return; -             } in             if(Sum = =target) -             { tolist<integer> add =NewArraylist<integer>(sublist); +                 if( !result.contains (add)) -Result.add (NewArraylist<integer>(sublist)); the                 return; *             } $             Panax Notoginseng             intLen =candidates.length; -              for(inti = begin; i < Len; i++) the             { +Sum + =Candidates[i]; A Sublist.add (Candidates[i]); theGetcombinationsum (candidates, i + 1, sum, target, sublist, result); +Sublist.remove (Sublist.size ()-1); -Sum-=Candidates[i]; $             } $}

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.