- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem17.java
More file actions
Latest commit
37 lines (33 loc) · 1.16 KB
/
Copy pathProblem17.java
File metadata and controls
37 lines (33 loc) · 1.16 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
importjava.util.*;
classProblem17 {
publicstaticvoidmain(String[] args) {
System.out.println(newProblem17().letterCombinations("9"));
}
publicList<String> letterCombinations(Stringdigits) {
List<String> res = newArrayList<>();
if (digits.length() ==0) returnres;
charc = digits.charAt(0);
char[] arr = null;
charpos = (char)('a' + 3 * (c-'2'));
if(c == '7' ) {
arr = newchar[]{(char)pos, (char)(pos+1), (char)(pos+2), (char)(pos+3)};
}else
if(c == '8') {
arr = newchar[]{(char)(pos+1), (char)(pos+2), (char)(pos+3)};
} else
if(c == '9') {
arr = newchar[]{(char)(pos+1), (char)(pos+2), (char)(pos+3),(char)(pos+4)};
} elsearr = newchar[]{(char)pos, (char)(pos+1), (char)(pos+2)};
List<String> resSub = letterCombinations(digits.substring(1));
for (inti = 0; i < arr.length; i++) {
if(resSub.size() > 0) {
for (intj = 0; j < resSub.size(); j++) {
res.add(String.valueOf(arr[i]) + resSub.get(j));
}
} else {
res.add(String.valueOf(arr[i]));
}
}
returnres;
}
}