前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >17. 电话号码的字母组合

17. 电话号码的字母组合

作者头像
张伦聪zhangluncong
发布2022-10-26 18:18:12
2810
发布2022-10-26 18:18:12
举报

给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。

给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

示例:

代码语言:javascript
复制
输入:"23"
输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

说明:

  • 尽管上面的答案是按字典序排列的,但是你可以任意选择答案输出的顺序。

解:非常典型的dfs,以23为例,树的第一层有a,b,c三个节点,第二层有d,e,f三个节点,开始深度遍历。

代码语言:javascript
复制
class Solution {
    public List<String> letterCombinations(String digits) {
        List<String> res = new ArrayList<>();
        if (digits == null || digits.length() == 0) {
            return res;
        }
        Map<Character, String[]> map = new HashMap<>();
        map.put('2', new String[]{"a", "b", "c"});
        map.put('3', new String[]{"d", "e", "f"});
        map.put('4', new String[]{"g", "h", "i"});
        map.put('5', new String[]{"j", "k", "l"});
        map.put('6', new String[]{"m", "n", "o"});
        map.put('7', new String[]{"p", "q", "r", "s"});
        map.put('8', new String[]{"t", "u", "v"});
        map.put('9', new String[]{"w", "x", "y", "z"});
        StringBuilder sb = new StringBuilder();
        dfs(digits, 0, map, res, sb);
        return res;
    }

    private void dfs(String digits, int step, Map<Character, String[]> map, List<String> res, StringBuilder sb) {
        if (step == digits.length()) {
            res.add(sb.toString());
            return;
        }
        char c = digits.charAt(step);
        String[] tmp = map.get(c);
        for (int i = 0; i < tmp.length; i++) {
            sb.append(tmp[i]);
            dfs(digits, step + 1, map, res, sb);
            sb.deleteCharAt(sb.length() - 1);
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-09-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档