美文网首页
17. Letter Combinations of a Pho

17. Letter Combinations of a Pho

作者: 夜皇雪 | 来源:发表于2016-11-27 04:09 被阅读0次
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++){
            int x = Character.getNumericValue(digits.charAt(i));
            while(ans.peek().length()==i){
                String t = ans.remove();
                for(char s : mapping[x].toCharArray())
                    ans.add(t+s);
            }
        }
        return ans;
    }
}

相关文章

网友评论

      本文标题:17. Letter Combinations of a Pho

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