【leetcode】电话号码的字母组合

题目:

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

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

示例:

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

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


 

 

解法一:迭代法

将原数组中的值加上当前值得可能性进行组合,

比如第一个数字2,对应的就是abc,这时 候数组中的值就是[a,b,c]

轮到下一个数字3,对应的是def,就把a拼上d/e/f,b拼上d/e/f,c拼上d/e/f, 返回数组

java代码:

class Solution {
    public List<String> letterCombinations(String digits) {
        HashMap<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"});

        int len = digits.length();
        List<String> res = new ArrayList<>();
        for (int i = 0; i < len; i++) {
            String[] tmp = map.get(digits.charAt(i));
            List<String> listTmp = new ArrayList<>();
            if (i == 0) {
                for (String s : tmp) {
                    res.add(s);
                }
            } else {
                for (int j = 0; j < res.size(); j++) {
                    for (String s : tmp) {
                        listTmp.add(res.get(j) + s);
                    }
                }
                res = listTmp;
            }
        }

        return res;
    }
}

 

解法二:回溯法

把每个数字字符当作递归的一层,每一层中先枚举一个字母,

递归进入下一层,再删除这个字母,回到上一个状态,枚举下一个字母。

递归结束标志是递归了digits.length层,即字母组合长度等于digits长度,

递归结束得到一个符合的字母组合,加入list。等于是在循环中套递归

java代码:

class Solution {
    public List<String> letterCombinations(String digits) {
        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"});

        int len = digits.length();
        List<String> res = new ArrayList<>();

        if (len == 0) {
            return res;
        }

        String oneRes = "";
        combi(digits, 0, res, oneRes, map);

        return res;
    }

    private void combi(String digits, int i, List<String> res, String oneRes, Map<Character, String[]> map) {
        if (i == digits.length()) {
            res.add(oneRes);
            return;
        }

        String[] tmp = map.get(digits.charAt(i));
        for (String s : tmp) {
            oneRes += s; //把这个字母加入oneRes
            combi(digits, i + 1, res, oneRes, map);
            oneRes = oneRes.substring(0, oneRes.length() - 1);//把s从oneRes中删除(这个如果不好理解,可以举个例子23,在纸上走一遍)
        }
    }
}

 

由于水平有限,博客中难免会有一些错误,有纰漏之处恳请各位大佬不吝赐教!

 

推荐阅读:

【leetcode-动态规划】爬楼梯  - CSDN博客

【leetcode-动态规划】买卖股票的最佳时机  - CSDN博客

【leetcode-动态规划】最大子序和 

【leetcode-动态规划】 不同路径  - CSDN博客

【leetcode-动态规划】打家劫舍 

【leetcode-动态规划】 跳跃游戏  - CSDN博客

【leetcode-动态规划】零钱兑换  - CSDN博客

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是电话号码字母组合问题的 C++ 代码实现: ``` class Solution { public: vector<string> letterCombinations(string digits) { unordered_map<char, string> mapping = { {'2', "abc"}, {'3', "def"}, {'4', "ghi"}, {'5', "jkl"}, {'6', "mno"}, {'7', "pqrs"}, {'8', "tuv"}, {'9', "wxyz"} }; vector<string> res; if (digits.empty()) { return res; } string combination; backtrack(res, combination, digits, 0, mapping); return res; } void backtrack(vector<string>& res, string& combination, string& digits, int index, unordered_map<char, string>& mapping) { if (index == digits.size()) { res.push_back(combination); return; } for (char c : mapping[digits[index]]) { combination.push_back(c); backtrack(res, combination, digits, index + 1, mapping); combination.pop_back(); } } }; ``` 其中 `letterCombinations` 函数用来生成所有的字母组合,`backtrack` 函数用来进行回溯操作。在 `letterCombinations` 函数中,首先根据数字字符和字母的映射关系创建了一个 `unordered_map` 对象 `mapping`。然后定义了一个空字符串 `combination` 和一个空 vector `res` 来保存最终结果。最后调用了 `backtrack` 函数来生成所有的字母组合。在 `backtrack` 函数中,首先判断是否达到了数字字符串的末尾,如果是,则将当前的 `combination` 字符串保存到 `res` 中。否则,遍历当前数字字符所能表示的所有字母,依次加入到 `combination` 字符串中,然后递归调用 `backtrack` 函数,添加下一个数字字符所能表示的字母。递归完成后,需要将 `combination` 字符串还原到上一个状态,以便进行下一次回溯。最终返回 `res` 数组即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值