leetcode-17. 电话号码的字母组合

原始思路

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Solution {
List<String> result = new ArrayList<>();
StringBuffer sb = new StringBuffer();
public List<java.lang.String> letterCombinations(String digits) {
//初始化数组,输入如23
String[] arr = new String[]{"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
int[] chr = new int[digits.length()];
for (int i = 0; i < digits.length(); i++){
chr[i] = Integer.parseInt(java.lang.String.valueOf(digits.charAt(i)));
}
dfs(arr, chr, 0);
return result;
}

public void dfs(java.lang.String[] arr, int[] chr, int chrIndex){
if (sb.length() == chr.length){
if (sb.length() > 0){
result.add(sb.toString());
}
return;
}

if (chrIndex > chr.length){
return;
}
int begin = 0;
int end = 0;
if (chr[chrIndex] == 7){
begin = (chr[chrIndex]-2)*3;
end = (chr[chrIndex]-1)*3 ;
}else if (chr[chrIndex] == 8){
begin = (chr[chrIndex]-2)*3 + 1;
end = (chr[chrIndex]-1)*3 ;
}else if (chr[chrIndex] == 9){
begin = (chr[chrIndex]-2)*3 + 1;
end = arr.length -1;
}else{
begin = (chr[chrIndex]-2)*3;
end = (chr[chrIndex]-1)*3 - 1;
}

for (int i = begin; i<= end; i++){
sb.append(arr[i]);
dfs(arr, chr, chrIndex + 1);
sb.deleteCharAt(sb.length() - 1);
}
}
}