classSolution{List<List<Integer>> res =newArrayList();Deque<Integer> path =newArrayDeque();publicList<List<Integer>>combine(int n,int k){combineHelper(n, k,1);return res;}privatevoidcombineHelper(int n,int k,int startIndex){if(path.size()== k){res.add(newArrayList<>(path));return;}for(int i = startIndex; i <= n -(k - path.size())+1; i++){path.addFirst(i);combineHelper(n, k, i +1);path.removeFirst();}}}
216.组合总和III
classSolution{List<List<Integer>> res =newArrayList();Deque<Integer> path =newArrayDeque();int sum =0;publicList<List<Integer>>combinationSum3(int k,int n){combinationSum3Helper(k, n,1);return res;}privatevoidcombinationSum3Helper(int k,int n,int startIndex){if(path.size()== k){if(sum == n){res.add(newArrayList<>(path));}return;}for(int i = startIndex; i <=9; i++){if(sum + i > n)break;sum += i;path.addFirst(i);combinationSum3Helper(k, n, i +1);path.removeFirst();sum -= i;}}}
39. 组合总和
classSolution{List<List<Integer>> res =newArrayList();Deque<Integer> path =newArrayDeque();int sum =0;publicList<List<Integer>>combinationSum(int[] candidates,int target){if(candidates ==null|| candidates.length ==0)return res;Arrays.sort(candidates);combinationSumHelper(candidates, target,0);return res;}privatevoidcombinationSumHelper(int[] candidates,int target,int startIndex){if(sum == target){res.add(newArrayList<>(path));return;}for(int i = startIndex; i < candidates.length; i++){if(sum + candidates[i]> target)break;sum += candidates[i];path.addFirst(candidates[i]);combinationSumHelper(candidates, target, i);path.removeFirst();sum -= candidates[i];}}}
40.组合总和II
classSolution{List<List<Integer>> res =newArrayList();Deque<Integer> path =newArrayDeque();int sum =0;publicList<List<Integer>>combinationSum2(int[] candidates,int target){if(candidates ==null|| candidates.length ==0)return res;Arrays.sort(candidates);combinationSum2Helper(candidates, target,0);return res;}privatevoidcombinationSum2Helper(int[] candidates,int target,int startIndex){if(sum == target){res.add(newArrayList<>(path));return;}for(int i = startIndex; i < candidates.length; i++){if(i > startIndex && candidates[i]== candidates[i -1])continue;if(sum + candidates[i]> target)break;sum += candidates[i];path.addFirst(candidates[i]);combinationSum2Helper(candidates, target, i +1);path.removeFirst();sum -= candidates[i];}}}
17.电话号码的字母组合
classSolution{List<String> res =newArrayList();String[] numString ={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};StringBuilder builder =newStringBuilder();publicList<String>letterCombinations(String digits){if(digits ==null|| digits.length()==0){return res;}letterCombinationsHelper(digits,0);return res;}privatevoidletterCombinationsHelper(String digits,int index){if(index == digits.length()){res.add(builder.toString());return;}String s = numString[digits.charAt(index)-'0'];for(int i =0; i < s.length(); i++){builder.append(s.charAt(i));letterCombinationsHelper(digits, index +1);builder.deleteCharAt(builder.length()-1);}}}
78.子集
classSolution{List<List<Integer>> res =newArrayList();Deque<Integer> path =newArrayDeque();publicList<List<Integer>>subsets(int[] nums){subsetsHelper(nums,0);return res;}privatevoidsubsetsHelper(int[] nums,int startIndex){res.add(newArrayList<>(path));for(int i = startIndex; i < nums.length; i++){path.addFirst(nums[i]);subsetsHelper(nums, i +1);path.removeFirst();}}}
🌷🍁 博主猫头虎(🐅🐾)带您 Go to New World✨🍁 🦄 博客首页——🐅🐾猫头虎的博客🎐 🐳 《面试题大全专栏》 🦕 文章图文…