17. Letter Combinations of a Phone Number

Posted lawrenceseattle

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了17. Letter Combinations of a Phone Number相关的知识,希望对你有一定的参考价值。

Given a string containing digits from 2-9 inclusive, 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. Note that 1 does not map to any letters.



Example:

Input: "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
public class Solution {
    public List<String> letterCombinations(String digits) {
        LinkedList<String> ans = new LinkedList<String>();
        if(digits.length() == 0) return ans;
        String[] mapping = new String[] {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        ans.add("");
        for(int i = 0; i < digits.length(); i++){
            //得到那个数字 3
            int x = Character.getNumericValue(digits.charAt(i));
            // ans.peek().length()==i 说明还没有更新好
            while(ans.peek().length()==i){
                String t = ans.remove();
                // 3 对应的 a b c, 在弹出的所有的后面都加上a b c,再加回去
                for(char s : mapping[x].toCharArray())
                    ans.add(t+s);
            }
        }
        return ans;
    }
}

以上是关于17. Letter Combinations of a Phone Number的主要内容,如果未能解决你的问题,请参考以下文章

17. Letter Combinations of a Phone Number

17. Letter Combinations of a Phone Number

17. Letter Combinations of a Phone Number

17. Letter Combinations of a Phone Number

17. Letter Combinations of a Phone Number

17. Letter Combinations of a Phone Number