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 3542a46

Browse files
Merge pull request #80 from bluesword12350/master
017. Letter Combinations of a Phone Number (java)
2 parents 39222f0 + 7af9bce commit 3542a46

File tree

1 file changed

+30
-0
lines changed
  • solution/017. Letter Combinations of a Phone Number

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public List<String> letterCombinations(String digits) {
3+
char[] cs = digits.toCharArray();
4+
List<String> result = new ArrayList<>();
5+
for (char a : cs) {
6+
char[] charArray;
7+
switch (a) {
8+
case '2': charArray = new char[]{'a','b','c'}; break;
9+
case '3': charArray = new char[]{'d','e','f'}; break;
10+
case '4': charArray = new char[]{'g','h','i'}; break;
11+
case '5': charArray = new char[]{'j','k','l'}; break;
12+
case '6': charArray = new char[]{'m','n','o'}; break;
13+
case '7': charArray = new char[]{'p','q','r','s'}; break;
14+
case '8': charArray = new char[]{'t','u','v'}; break;
15+
case '9': charArray = new char[]{'w','x','y','z'}; break;
16+
default: return null;
17+
}
18+
if (result.size() == 0) {
19+
for (char aCharArray : charArray) result.add(String.valueOf(aCharArray));
20+
} else {
21+
List<String> cache = new ArrayList<>();
22+
for (String string : result) {
23+
for (char aCharArray : charArray) cache.add(string + aCharArray);
24+
}
25+
result = cache;
26+
}
27+
}
28+
return result;
29+
}
30+
}

0 commit comments

Comments
(0)

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