电话号码字母组合
约 264 字小于 1 分钟
2024-12-15
电话号码的字母组合
给定一个仅包含数字 2-9
的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。 给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
string MAPPING[10] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
示例 1:
输入:digits = "23"
输出:["ad","ae","af","bd","be","bf","cd","ce","cf"]
示例 2:
输入:digits = ""
输出:[]
示例 3:
输入:digits = "2"
输出:["a","b","c"]
这题思路也和之前一样,只是要注意的是,这里的枚举是枚举当前选哪个,而不是枚举选或不选。
class Solution {
string MAPPING[10] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
public:
vector<string> letterCombinations(string digits) {
int n =digits.size();
if(n==0) return {};
string temp(n,' ');
vector<string> res;
function<void(int)> dfs=[&](int i){
if(i>n) return ;
if(i==n){
res.push_back(temp);
return;
}
//枚举当前选哪个
for(char c : MAPPING[digits[i] - '0']){
temp[i] = c;
fs(i+1);
}
};
dfs(0);
return res;
}
};
更新日志
2024/12/15 12:44
查看所有更新日志
64700
-update leetcode于