The original title link is here: https://leetcode.com/problems/combination-sum-iii/
Similar to combination Sum II, the difference is that not all elements are added, except that the K elements are added.
Therefore, it is necessary to satisfy the item.size () = = k and target = = 2 conditions before adding the copy of item to Res.
AC Java:
1 Public classSolution {2 PublicList<list<integer>> combinationSum3 (intKintN) {3list<list<integer>> res =NewArraylist<list<integer>>();4 if(k <= 0 | | n <=0){5 returnRes;6 }7 int[] candidates = {1,2,3,4,5,6,7,8,9};8Helper (K,n,0,candidates,NewArraylist<integer>(), res);9 returnRes;Ten } One Private voidHelperintKintNintStartint[] candidates, list<integer> item, list<list<integer>>Res) { A if(item.size () = = k && n==0){ -Res.add (NewArraylist<integer>(item)); - return; the } - if(N<0 | | item.size () >k) { - return; - } + for(inti = start; i<candidates.length; i++){ - Item.add (Candidates[i]); +Helper (k,n-candidates[i],i+1, candidates,item,res); AItem.remove (Item.size ()-1); at } - } -}
Leetcode Combination Sum III