20230130 顺利通过,是最简单的模板dfs题
20230201 顺利通过
20230204 顺利通过
20230218 dfs要return size为0要返回空字符串。
20230227 顺利通过
原题解
题目
约束
解法
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();
}
}
}
};
class Solution {
public:
string tmp;
vector<string> res;
vector<string> board={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
void DFS(int pos,string digits){
if(pos==digits.size()){
res.push_back(tmp);
return;
}
int num=digits[pos]-'0';
for(int i=0;i<board[num].size();i++){
tmp.push_back(board[num][i]);
DFS(pos+1,digits);
tmp.pop_back();
}
}
vector<string> letterCombinations(string digits) {
if(digits.size()==0) return res;
DFS(0,digits);
return res;
}
};
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)