-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode17.txt
More file actions
24 lines (22 loc) · 904 Bytes
/
leetcode17.txt
File metadata and controls
24 lines (22 loc) · 904 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Java
class Solution {
public static List<String> letterCombinations(String digits) {
String[] digitletter = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; 注意数组初始化的方式
List<String> res = new ArrayList<String>();
if(digits.length() == 0) return res;
res.add(""); //必须先加入“” 才能有后续的叠加
for(int i = 0;i < digits.length();i++) {
res = combination(digitletter[digits.charAt(i) - '0'], res);
}
return res;
}
public static List<String> combination(String digits,List<String> l){
List<String> result = new ArrayList<>();
for(int i = 0;i < digits.length();i++) {
for(String x:l) { //若按照下标索取,则需要用l.get(),来表示ArrayList中的元素
result.add(x + digits.charAt(i));
}
}
return result;
}
}