美文网首页
[Day6]17. Letter Combinations of

[Day6]17. Letter Combinations of

作者: Shira0905 | 来源:发表于2017-02-05 00:26 被阅读0次

    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.

    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<String> letterCombinations(String digits) {
        HashMap<Integer, Character[]> map=new HashMap<Integer,Character[]>();
        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<String> result =new ArrayList<String>();
        List<String> temp =new ArrayList<String>();
        for(int i=0;i<digits.length();i++){
            temp.clear();
            int l=result.size();
            temp.addAll(result);
            result.clear();
            int d=Integer.valueOf(digits.charAt(i)+"");
            Character[]chars=map.get(d);
            if(l==0){
                for(int j=0;j<chars.length;j++){
                    result.add(chars[j]+"");
                }
            }else{
                for(int j=0;j<chars.length;j++){
                    for(int k=0;k<l;k++){
                        result.add(temp.get(k)+chars[j]);
                    }
                }
            }
        }
        return result;
    }

    相关文章

      网友评论

          本文标题:[Day6]17. Letter Combinations of

          本文链接:https://www.haomeiwen.com/subject/jovsittx.html