码迷,mamicode.com
首页 > 其他好文 > 详细

17. Letter Combinations of a Phone Number

时间:2019-01-14 10:48:53      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:character   put   amp   note   ret   def   没有   lin   linked   

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

标签:character   put   amp   note   ret   def   没有   lin   linked   

原文地址:https://www.cnblogs.com/lawrenceSeattle/p/10265250.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!