-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhoneNumber.java
54 lines (52 loc) · 1.19 KB
/
PhoneNumber.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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class PhoneNumber {
public static List<String> letterCombinations(String digits) {
List<String> result = new ArrayList<String>();
char[] c = digits.toCharArray();
for (int i = 0; i < c.length; i++) {
List<String> perNum = new ArrayList<String>();
switch (c[i]) {
case '2':
perNum = Arrays.asList("a", "b", "c");
break;
case '3':
perNum = Arrays.asList("d", "e", "f");
break;
case '4':
perNum = Arrays.asList("g", "h", "i");
break;
case '5':
perNum = Arrays.asList("j", "k", "l");
break;
case '6':
perNum = Arrays.asList("m", "n", "o");
break;
case '7':
perNum = Arrays.asList("p", "q", "r", "s");
break;
case '8':
perNum = Arrays.asList("t", "u", "v");
break;
case '9':
perNum = Arrays.asList("w", "x", "y", "z");
break;
default:
break;
}
if (result.isEmpty()) {
result = perNum;
continue;
}
List<String> newList = new ArrayList<String>();
for (String string : result) {
for (String cha : perNum) {
newList.add(string + cha);
}
}
result = newList;
}
return result;
}
}