美文网首页
Letter Combinations of a Phone N

Letter Combinations of a Phone N

作者: nafoahnaw | 来源:发表于2018-03-11 18:03 被阅读0次

    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.

    image

    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.

    大意就是返回给出数字的对应手机键盘上的所有可能的组合

      public List<String> letterCombinations(String digits) {
            LinkedList<String> result = new LinkedList<String>();
            if(digits == null || digits.length() == 0){
                return result;
            }
    
            String[] buttons = new String[]{"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
            result.add("");
            /**
             * 利用FIFO
             */
            for(int i = 0; i < digits.length(); i++){
                int c = Character.getNumericValue(digits.charAt(i));
    
    
                while (result.peek().length() == i){
                    /**
                     * 顶端弹出栈
                     */
                    String p = result.remove();
                    for(char cr : buttons[c].toCharArray()){
                        /**
                         * 入栈,在队列尾端增加
                         */
                        result.offer(p + cr);
                    }
                }
            }
            return result;
        }
    

    相关文章

      网友评论

          本文标题:Letter Combinations of a Phone N

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