你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

Leecode刷题热题HOT100(17)——电话号码的字母组合

2021/12/14 23:39:05

Leecode刷题HOT100系列文章目录

题目

给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。

给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
在这里插入图片描述

示例 1:

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

示例 2:

输入:digits = ""
输出:[]

示例 3:

输入:digits = "2"
输出:["a","b","c"]

提示:

  • 0 <= digits.length <= 4
  • digits[i] 是范围 [‘2’, ‘9’] 的一个数字。

解题思路

方法一:回溯法

回溯过程中维护一个字符串,表示已有的字母排列(如果未遍历完电话号码的所有数字,则已有的字母排列是不完整的)。该字符串初始为空。每次取电话号码的一位数字,从哈希表中获得该数字对应的所有可能的字母,并将其中的一个字母插入到已有的字母排列后面,然后继续处理电话号码的后一位数字,直到处理完电话号码中的所有数字,即得到一个完整的字母排列。然后进行回退操作,遍历其余的字母排列。

我的答案

class Solution {
public:
    void backtrack(unordered_map<char,string> &phone,vector<string> &result,int index,const string &digits,string &tempStr){
        if(index == digits.size()) {    // 表示只剩最后一位
            result.push_back(tempStr);
        } else {
            char ch = digits[index];    // 当前数字
            const string letters = phone.find(ch)->second;  // 当前数字的字母
            for(char c :letters){       // 当前数字的每一个字母,回溯
                tempStr.push_back(c);
                backtrack(phone,result,index+1,digits,tempStr); 
                tempStr.pop_back();
            }
        }
    }
    vector<string> letterCombinations(string digits) {
        vector<string> result;
        unordered_map<char,string> phone = {
            {'2',"abc"},
            {'3',"def"},
            {'4',"ghi"},
            {'5',"jkl"},
            {'6',"mno"},
            {'7',"pqrs"},
            {'8',"tuv"},
            {'9',"wxyz"}
        };
        int len = digits.size();
        if(len == 0) {
            return result;
        }
        string tempStr;         // 存储过程中的不完整字符串
        backtrack(phone,result,0,digits,tempStr); 
        return result;
    }
};

优质解答

class Solution {
public:
    vector<string> letterCombinations(string digits) {
        vector<string> combinations;
        if (digits.empty()) {
            return combinations;
        }
        unordered_map<char, string> phoneMap{
            {'2', "abc"},
            {'3', "def"},
            {'4', "ghi"},
            {'5', "jkl"},
            {'6', "mno"},
            {'7', "pqrs"},
            {'8', "tuv"},
            {'9', "wxyz"}
        };
        string combination;
        backtrack(combinations, phoneMap, digits, 0, combination);
        return combinations;
    }

    void backtrack(vector<string>& combinations, const unordered_map<char, string>& phoneMap, const string& digits, int index, string& combination) {
        if (index == digits.length()) {
            combinations.push_back(combination);
        } else {
            char digit = digits[index];
            const string& letters = phoneMap.at(digit);
            for (const char& letter: letters) {
                combination.push_back(letter);
                backtrack(combinations, phoneMap, digits, index + 1, combination);
                combination.pop_back();
            }
        }
    }
};

知识积累


都看到这了,帮忙点个赞吧!!!
注:本文题目描述及优质解答来自力扣网

我吃得下一个超市,一群牲口,吃得下一群梦想和野心。——《饥饿是很好的锻炼》