Given two integers n and k, return all possible combinations ofknumbers chosen from the range[1, n].
You may return the answer in any order.
Example 1:
Input: n = 4, k = 2 Output: [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]] Explanation: There are 4 choose 2 = 6 total combinations. Note that combinations are unordered, i.e., [1,2] and [2,1] are considered to be the same combination.
Example 2:
Input: n = 1, k = 1 Output: [[1]] Explanation: There is 1 choose 1 = 1 total combination.
Find all valid combinations of k numbers that sum up to n such that the following conditions are true:
Only numbers 1 through 9 are used.
Each number is used at most once.
Return a list of all possible valid combinations. The list must not contain the same combination twice, and the combinations may be returned in any order.
Example 1:
Input: k = 3, n = 7 Output: [[1,2,4]] Explanation: 1 + 2 + 4 = 7 There are no other valid combinations.
Example 2:
Input: k = 3, n = 9 Output: [[1,2,6],[1,3,5],[2,3,4]] Explanation: 1 + 2 + 6 = 9 1 + 3 + 5 = 9 2 + 3 + 4 = 9 There are no other valid combinations.
Example 3:
Input: k = 4, n = 1 Output: [] Explanation: There are no valid combinations. Using 4 different numbers in the range [1,9], the smallest sum we can get is 1+2+3+4 = 10 and since 10 > 1, there are no valid combination.
Constraints:
2 <= k <= 9
1 <= n <= 60
正常递归
classSolution { public List<List<Integer>> combinationSum3(int k, int n) { List<List<Integer>> resultList = newArrayList<>(); List<Integer> list = newLinkedList<>(); backtracing(k,n,1,resultList,list); return resultList; } publicvoidbacktracing(int k,int n ,int cur,List<List<Integer>> resultList,List<Integer> list){ if(list.size()==k&&getSum(list)==n) { resultList.add(newArrayList(list)); } for(int i=cur;i<=9;i++){ list.add(i); backtracing(k,n,i+1,resultList,list); list.removeLast(); } } public Integer getSum(List<Integer> list){ intsum=0; for(inti=0;i<list.size();i++){ sum += list.get(i); } return sum; } }
剪枝优化
classSolution { public List<List<Integer>> combinationSum3(int k, int n) { List<List<Integer>> resultList = newArrayList<>(); List<Integer> list = newLinkedList<>(); backtracing(0,k,n,1,resultList,list); return resultList; } publicvoidbacktracing(int sum,int k,int n ,int cur,List<List<Integer>> resultList,List<Integer> list){ if (sum>n) return ; if(list.size()==k){ if(sum==n){ resultList.add(newArrayList<>(list)); return; } } for(inti=cur;i<=9-(k-list.size())+1;i++){ sum += i; list.add(i); backtracing(sum,k,n,i+1,resultList,list); list.remove(list.size()-1); sum -= i ; } } }
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.
A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations ofcandidateswhere the chosen numbers sum totarget. You may return the combinations in any order.
The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the
frequency
of at least one of the chosen numbers is different.
The test cases are generated such that the number of unique combinations that sum up to target is less than 150 combinations for the given input.
Example 1:
Input: candidates = [2,3,6,7], target = 7 Output: [[2,2,3],[7]] Explanation: 2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times. 7 is a candidate, and 7 = 7. These are the only two combinations.