-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhoneNumber.java
More file actions
46 lines (39 loc) · 1.2 KB
/
Copy pathPhoneNumber.java
File metadata and controls
46 lines (39 loc) · 1.2 KB
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
package leetcode;
/**
* Created by helmeter on 6/13/16.
*/
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class PhoneNumber {
private String[] alpha = new String[] {
" ",
"1", "abc", "def",
"ghi", "jkl", "mno",
"pqrs", "tuv", "wxyz"
};
private StringBuilder word;
private void dfs(List<String> res, String digits, int cur) {
if (cur >= digits.length()) {
res.add(word.toString());
} else {
for (int i = 0; i < alpha[digits.charAt(cur) - '0'].length(); ++i) {
word.append(alpha[digits.charAt(cur) - '0'].charAt(i));
dfs(res, digits, cur + 1);
word.deleteCharAt(word.length() - 1);
}
}
}
public List<String> letterCombinations(String digits) {
List<String> ret = new ArrayList<String>();
word = new StringBuilder();
dfs(ret, digits, 0);
return ret;
}
// debug
public static void main(String[] args) {
PhoneNumber s = new PhoneNumber();
int[] input = {1, 2, 3, 1};
System.out.println(s.letterCombinations("235"));
}
}