Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0ea9c1b

Browse files
Add Solution2.java to problems 0017
1 parent 95a978c commit 0ea9c1b

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution {
2+
3+
public List<String> letterCombinations(String digits) {
4+
Map<String, String> map = new HashMap<>();
5+
for (int i = 2, index = 0; i < 7; i ++) {
6+
for (int j = 0; j < 3; j ++) {
7+
map.put(String.valueOf(i) + String.valueOf(j), (char)('a' + (index++)) + "");
8+
}
9+
}
10+
map.put("70", "p");
11+
map.put("71", "q");
12+
map.put("72", "r");
13+
map.put("73", "s");
14+
15+
map.put("80", "t");
16+
map.put("81", "u");
17+
map.put("82", "v");
18+
19+
map.put("90", "w");
20+
map.put("91", "x");
21+
map.put("92", "y");
22+
map.put("93", "z");
23+
List<String> res = new ArrayList<>();
24+
if (digits.length() > 0) {
25+
solution(res, map, digits, "", 0);
26+
}
27+
return res;
28+
}
29+
30+
private void solution(List<String> res, Map<String, String> map, String digits, String target, int index) {
31+
if (index == digits.length()) {
32+
res.add(target);
33+
return;
34+
}
35+
String snet = String.valueOf(digits.charAt(index));
36+
int length = ("7".equals(snet) || "9".equals(snet)) ? 4 : 3;
37+
for (int i = 0; i < length; i++) {
38+
solution(res, map, digits, target + map.get(snet + i), index + 1);
39+
}
40+
}
41+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /