-
Notifications
You must be signed in to change notification settings - Fork 29
/
Solution17.java
69 lines (56 loc) · 1.77 KB
/
Solution17.java
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package backstracking_problem;
import java.util.ArrayList;
import java.util.List;
/**
* 递归与回溯
* 树形问题 17:
* 1、字符串的合法性
* 2、空字符串(null)
* 3、多个解的顺序(无)
* 4、digits 是数字字符串,s(digits) 是digits所能代表的字母字符串,
* s(digital[0...n-1])
* = letter(digital[0]) + letter(digital[1...n-1])
* = letter(digital[0]) + letter(digital[1]) + letter(digital[2...n-1])
* = ...
* 5、递归调用的一个重要特征:要返回——回溯,它是暴力解法的一个主要实现手段。
* 6、3^n = O(2^n)
* O(2^len(s))
* O(len(s)
*/
public class Solution17 {
private String letterMap[] = {
" ", //0
"", //1
"abc", //2
"def", //3
"ghi", //4
"jkl", //5
"mno", //6
"pqrs", //7
"tuv", //8
"wxyz" //9
};
private ArrayList<String> res;
public List<String> letterCombinations(String digits) {
res = new ArrayList<>();
if (digits == null || digits.equals("")) {
return res;
}
findCombinations(digits, 0, "");
return res;
}
private void findCombinations(String digits, int index, String s) {
if (digits.length() == index) {
res.add(s);
return;
}
Character c = digits.charAt(index);
assert c.compareTo('0') >= 0 &&
c.compareTo('9') <= 0 &&
c.compareTo('1') != 0;
String letter = letterMap[c - '0'];
for (int i = 0; i < letter.length(); i++) {
findCombinations(digits, index + 1, s + letter.charAt(i));
}
}
}