Combination Sum II
Problem:
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.
Ideas:
Common backtracking problems
My Code:
Public classSolution { PublicList<list<integer>> combinationSum2 (int[] num,inttarget) { if(num = =NULL|| Num.length = = 0)returnrst; List<Integer> list =NewArraylist<integer>(); Arrays.sort (num); Helper (list, num, target,0, 0); returnrst; } PrivateList<list<integer>> rst =NewArraylist<list<integer>>(); Public voidHelper (list<integer> List,int[] candidates,intTargetintSumintstart) { if(Sum > Target)return; if(Sum = =target) { if(!rst.contains (list)) Rst.add (NewArrayList (list)); return; } for(inti = start; i < candidates.length; i++) {List.add (candidates[i]); Helper (list, candidates, target, sum+ Candidates[i], i + 1); List.remove (List.size ()+ W); } }}View Code
Combination Sum II