[Day6]17. Letter Combinations of a Phone Number

I am so sorry for my late for Feb. 4th's problem. Now it is already Feb. 4th.
This is another MEDIUM level, but for me, it is easier than yesterday's problem.

By the way, I forgot to mention my first time to use 'debug' mode in myEclipse for [Day5] problem.
Awsome! So, I use it again! It really helpful for me to stay a clear mind.

DESCRIPTION:
Given a digit string, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below.

[Day6]17. Letter Combinations of a Phone Number_第1张图片
200px-Telephone-keypad2.svg.png

Input:Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Note:Although the above answer is in lexicographical order, your answer could be in any order you want.
Subscribe to see which companies asked this question.

ANALYSIS:

Just something like 'loop', 'add to list'. But I have to say that there are some details is not so easy to deal. First, the new variable 'temp' for restoring the old state of the 'result' , since the result would change but we need the same initial state of 'result' for each char in three or four of the digital char[]. Second, this is my first time to use '.clear()', maybe just simply use 'temp=result' is something like 'point to'? I am not sure for this...

SOLUTION:

public static List letterCombinations(String digits) {
    HashMap map=new HashMap();
    Character[]chars2={'a','b','c'};
    map.put(2, chars2);
    Character[]chars3={'d','e','f'};
    map.put(3, chars3);
    Character[]chars4={'g','h','i'}; 
    map.put(4, chars4);
    Character[]chars5={'j','k','l'};
    map.put(5, chars5);
    Character[]chars6={'m','n','o'};
    map.put(6, chars6);
    Character[]chars7={'p','q','r','s'};
    map.put(7, chars7);
    Character[]chars8={'t','u','v'};
    map.put(8, chars8);
    Character[]chars9={'w','x','y','z'};
    map.put(9, chars9);
    List result =new ArrayList();
    List temp =new ArrayList();
    for(int i=0;i

你可能感兴趣的:([Day6]17. Letter Combinations of a Phone Number)