提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> group = new ArrayList<>();
public List<List<Integer>> combinationSum3(int k, int n) {
fun(1, k, n, 0);
return res;
}
public void fun(int start, int k, int n, int count){
if(group.size() == k && count == n){
res.add(new ArrayList<>(group));
return ;
}
for(int i = start; i <= 9; i ++){
count += i;
group.add(i);
fun(i+1, k, n, count);
count -= i;
group.remove(group.size()-1);
}
}
}
class Solution {
String[] str = new String[]{"", "", "abc", "def", "ghi", "jkl", "mno", "qprs", "tuv", "wxyz"};
List<String> res = new ArrayList<>();
StringBuilder sb = new StringBuilder();
public List<String> letterCombinations(String digits) {
if(digits.length() == 0){
return res;
}
fun(digits, 0);
return res;
}
public void fun(String digits, int startInt){
if(startInt == digits.length()){
res.add(sb.toString());
return;
}
String s = str[digits.charAt(startInt) - '0'];
for(int i = 0; i < s.length(); i ++){
sb.append(s.charAt(i));
fun(digits, startInt+1);
sb.deleteCharAt(sb.length()-1);
}
}
}