216. 组合总和 III
LinkedList<Integer> path = new LinkedList<>();
List<List<Integer>> result = new ArrayList<>();
/**
* @param k 规模 k 个数
* @param n 目标 和为 n
* @return
*/
public List<List<Integer>> combinationSum3(int k, int n) {
backTracing3(k,n,0,0);
return result;
}
public void backTracing3(int k, int n, int startIndex,int sum) {
if(sum > n){
return;
}
if(path.size() > k){
return;
}
if (path.size() == k) {
if (sum == n) {
result.add(new ArrayList<>(path));
}
return;
}
for (int i = startIndex; i < 9; i++) {
path.add(i + 1);
sum += i + 1;
backTracing3(k, n, i + 1,sum);
sum -= i + 1;
path.removeLast();
}
}
17. 电话号码的字母组合
class Solution {
String[] list = {"","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
// list 存放数字对应路径
StringBuilder sb = new StringBuilder();
List<String> res = new ArrayList<>();
public List<String> letterCombinations(String digits) {
if("".equals(digits)){
return res;
}
backTracing(digits.length(),digits,0);
return res;
}
public void backTracing(int length,String digits,int index){
if(sb.length() == length){
res.add(String.valueOf(sb));
return;
}
String str = list[ digits.charAt(index) - '1' ];
for(int i = 0 ; i < str.length() ; i++){
sb.append(str.charAt(i));
index++;
backTracing(length,digits,index);
index--;
sb.deleteCharAt(sb.length()-1);
}
}
}
标签:digits,视频,return,int,sum,本事,length,path,两道
From: https://www.cnblogs.com/Chain-Tian/p/17029240.html