8000 Roughly commit · fibers/ex-algorithm@cc4f417 · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit cc4f417

Browse files
committed
Roughly commit
1 parent 6a0964f commit cc4f417

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed
Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,53 @@
11
package com.fibers.algorithm.leetcode._017;
2+
3+
import com.fibers.utils.Utils;
4+
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
210
public class Solution {
11+
Map<String, String> phone = new HashMap<String, String>() {{
12+
put("2", "abc");
13+
put("3", "def");
14+
put("4", "ghi");
15+
put("5", "jkl");
16+
put("6", "mno");
17+
put("7", "pqrs");
18+
put("8", "tuv");
19+
put("9", "wxyz");
20+
}};
21+
22+
public static void main(String[] args) {
23+
Solution s = new Solution();
24+
Utils.printList(s.letterCombinations("23"));
25+
}
26+
27+
28+
public List<String> letterCombinations(String digits) {
29+
List<String> result = new ArrayList<>();
30+
31+
if (digits.length() != 0) {
32+
backtrack(result, "", digits);
33+
}
34+
35+
return result;
36+
}
37+
38+
39+
public void backtrack(List<String> result, String combination, String digits) {
40+
41+
if (digits.length() == 0) {
42+
result.add(combination);
43+
} else {
44+
String d = digits.substring(0, 1);
45+
String letters = phone.get(d);
46+
47+
for (int i = 0; i < letters.length(); i++) {
48+
backtrack(result, combination + letters.substring(i, i + 1), digits.substring(1));
49+
}
50+
}
51+
}
352
}
4-
53+
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
package com.fibers.algorithm.leetcode._018;
2+
23
public class Solution {
4+
5+
36
}
4-
7+

src/main/java/com/fibers/utils/Utils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static void swap(List<Integer> list, int idx1, int idx2) {
4242
}
4343
}
4444

45-
public static void printList(List<Integer> l) {
45+
public static void printList(List<?> l) {
4646
if (l != null) {
4747

4848
System.out.print("[");

0 commit comments

Comments
 (0)
0