-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC_17.java
34 lines (30 loc) · 1.06 KB
/
LC_17.java
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
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class LC_17 {
static List<String> res = new ArrayList<>();
static StringBuilder path = new StringBuilder();
static String[] keys = new String[]{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
public static List<String> letterCombinations(String digits) {
if (digits.isEmpty()) return res;
backTrack(digits, 0);
return res;
}
public static void backTrack(String digits, int idx) {
if (idx == digits.length()) {
res.add(path.toString());
return;
}
String nowKey = keys[digits.charAt(idx) - '0'];
for (int i = 0; i < nowKey.length(); i++) {
path.append(nowKey.charAt(i));
backTrack(digits, idx + 1);
path.deleteCharAt(path.length() - 1);
}
}
public static void main(String[] args) {
String digits = "386459";
letterCombinations(digits);
System.out.println(res);
}
}