017 Letter Combinations of a Phone Number 电话号码的字母组合

给定一个数字字符串,返回数字所有可能表示的字母组合。

输入:数字字符串 "23"
输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

详见:https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/

实现语言:Java

class Solution {
    public List<String> letterCombinations(String digits) {
        List<String> result =new ArrayList<>();  
        if(digits == null || digits.isEmpty()){
            return result;
        }
        result.add("");    
        String []btns = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};    
        for(int i =0 ; i < digits.length() ;i++){  
            List<String> tmp = new ArrayList<>();  
            String letter = btns[digits.charAt(i)-'0'];  
            //遍历上一个列表,取出每一个元素,并和新的元素的每一个字符加起来保存  
            for(int j = 0 ; j < result.size();j++){  
                //遍历当前数字对应的所有字符  
                for(int k = 0; k< letter.length(); k++){  
                    tmp.add(result.get(j)+letter.charAt(k));  
                }  
            }  
            result = tmp;  
       }           
        return result;  
    }
}

 参考:https://vvaaiinn.iteye.com/blog/2208353

https://www.cnblogs.com/kepuCS/p/5271654.html

 

posted on 2018-03-31 22:20  lina2014  阅读(145)  评论(0编辑  收藏  举报

导航