-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLettersCombinationsOfaPhoneNumber.java
More file actions
45 lines (36 loc) · 1.04 KB
/
LettersCombinationsOfaPhoneNumber.java
File metadata and controls
45 lines (36 loc) · 1.04 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
public class Solution {
public ArrayList<String> letterCombinations(String digits) {
ArrayList<String> ret = new ArrayList<String>();
int numbers = 0;
try {
numbers = Integer.parseInt(digits);
} catch (Exception e) {
ret.add("");
return ret;
}
int high = 1;
while(numbers/high>10) {
high *= 10;
}
String temp = "";
hp(ret, numbers, high, temp);
return ret;
}
private void hp(ArrayList<String> ret, int numbers, int high, String temp) {
if(high == 0 ) {
ret.add(temp);
return;
}
int num = numbers/high;
numbers %= high;
high /=10;
int j=3;
if(num ==7|| num ==9) j=4;
for(int i=0; i<j; i++) {
int c = ('a' + 3*(num-2) + i);
if(num>7) c++;
char cc = (char)c;
hp(ret, numbers, high, temp+cc);
}
}
}