forked from hongtaocai/code_interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphonenumber.java
More file actions
executable file
·36 lines (34 loc) · 1.21 KB
/
phonenumber.java
File metadata and controls
executable file
·36 lines (34 loc) · 1.21 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
public class Solution {
public ArrayList<String> letterCombinations(String digits) {
// Start typing your Java solution below
// DO NOT write main() function
ArrayList<String> last = new ArrayList<String>();
ArrayList<String> now = new ArrayList<String>();
HashMap<Character, String> map = new HashMap<Character, String>();
map.put('2',"abc");
map.put('3',"def");
map.put('4',"ghi");
map.put('5',"jkl");
map.put('6',"mno");
map.put('7',"pqrs");
map.put('8',"tuv");
map.put('9',"wxyz");
last.add("");
for(int i=0;i<digits.length();i++){
if( (int)digits.charAt(i) <= (int) '9' &&
(int)digits.charAt(i) >= (int) '2' ){
for(String e:last){
String next = map.get(digits.charAt(i));
for(int m=0;m<next.length();m++){
now.add(e+next.charAt(m));
}
}
}
ArrayList<String> tmp = last;
last = now;
now = tmp;
now.clear();
}
return last;
}
}