首页 > 其他 > 详细

Leetcode-017-电话号码的字母组合

时间:2020-03-09 09:16:39      阅读:84      评论:0      收藏:0      [点我收藏+]

全排列问题。

class Solution {
    public List<String> letterCombinations(String digits) {
        Map<Character, String> dict = new HashMap<>();
        dict.put(‘2‘, "abc");
        dict.put(‘3‘, "def");
        dict.put(‘4‘, "ghi");
        dict.put(‘5‘, "jkl");
        dict.put(‘6‘, "mno");
        dict.put(‘7‘, "pqrs");
        dict.put(‘8‘, "tuv");
        dict.put(‘9‘, "wxyz");

        List<String> res = new ArrayList<>();
        if(digits.length()==0){return res;}
        res.add("");
        for(Character d:digits.toCharArray()){
            List<String> demo = new ArrayList<>();
            for(Character c:dict.get(d).toCharArray()){
                for(String s:res){
                    demo.add(new StringBuilder(s).append(c).toString());
                }
            }
            res = demo;
        }
        return res;
    }
}

 

class Solution:
    def letterCombinations(self, digits: str) -> List[str]:
        maps = {2: abc, 3: def, 4: ghi, 5: jkl, 6: mno, 7: pqrs, 8: tuv, 9: wxyz}
        if not digits:return []
        res = [‘‘]
        for d in digits:
            demo= []
            for c in maps[d]:
                demo += [ele + c for ele in res]
            res = demo
        return res

 

Leetcode-017-电话号码的字母组合

原文:https://www.cnblogs.com/huangzengrui/p/12446257.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!