-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathAC_simulation_n.java
47 lines (42 loc) · 1.3 KB
/
AC_simulation_n.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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_simulation_n.java
* Create Date: 2015-03-04 15:44:40
* Descripton:
*/
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Solution {
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) {
Scanner cin = new Scanner(System.in);
Solution s = new Solution();
int[] input = {1, 2, 3, 1};
System.out.println(s.letterCombinations("23"));
}
}