File tree 1 file changed +30
-0
lines changed
solution/017. Letter Combinations of a Phone Number
1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments