-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLeetCode_00017.java
More file actions
52 lines (48 loc) · 1.66 KB
/
LeetCode_00017.java
File metadata and controls
52 lines (48 loc) · 1.66 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
46
47
48
49
50
51
52
package com.github.jerring.leetcode;
import java.util.ArrayList;
import java.util.List;
public class LeetCode_00017 {
// public List<String> letterCombinations(String digits) {
// List<String> res = new ArrayList<>();
// if (digits == null || digits.length() == 0) {
// return res;
// }
// String[] dict = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
// dfs(new StringBuilder(), digits, res, dict);
// return res;
// }
//
// private void dfs(StringBuilder prefix, String digits, List<String> res, String[] dict) {
// if (prefix.length() == digits.length()) {
// res.add(prefix.toString());
// return;
// }
// int index = digits.charAt(prefix.length()) - '2';
// for (char c : dict[index].toCharArray()) {
// // 添加
// prefix.append(c);
// dfs(prefix, digits, res, dict);
// // 删除
// prefix.deleteCharAt(prefix.length() - 1);
// }
// }
public List<String> letterCombinations(String digits) {
List<String> res = new ArrayList<>();
if (digits == null || digits.length() == 0) {
return res;
}
res.add("");
String[] dict = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
for (char c : digits.toCharArray()) {
String s = dict[c - '2'];
List<String> tmp = new ArrayList<>();
for (String prefix : res) {
for (char cur : s.toCharArray()) {
tmp.add(prefix + cur);
}
}
res = tmp;
}
return res;
}
}