- Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSolution17.java
More file actions
Latest commit
executable file
·43 lines (37 loc) · 1.25 KB
/
Copy pathSolution17.java
File metadata and controls
executable file
·43 lines (37 loc) · 1.25 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
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;
publicclassSolution17 {
publicList<String> letterCombinations(Stringdigits) {
if (digits==null||digits.length()==0){
returnnewArrayList<>();}
Map<Character,String> map = newHashMap<>();
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");
Listres = newArrayList<>();
getCombinations("",digits,0,res,map);
returnres;
}
voidgetCombinations(Stringstr,Stringdigits,intposition,List<String> res,Map<Character,String> map){
if (digits.length()==position){
res.add(str);
return;
}
Stringletters = map.get(digits.charAt(position));
for (intj = 0;j<letters.length();j++){
//char和char不能直接相加,char+string可以,char+char返回的是int,ASCII对应整数
getCombinations(str+letters.charAt(j),digits,position+1,res,map);
}
}
publicstaticvoidmain(String[] args) {
Solution17s = newSolution17();
System.out.println(s.letterCombinations("23"));
}
}