易错点:
不加 if (len == 0) return ans; 的话,ans有一个空串,跟什么都没有不一样!!!
class Solution {
public:
string mp[10];
vector<string> ans;
void dfs(string &digits, int len, int index, string tmp) {
if (index == len) {
ans.push_back(tmp);
return ;
}
int n = digits[index] - '0';
for (int i = 0; i < mp[n].size(); ++i) {
tmp.push_back(mp[n][i]);
dfs(digits, len, index + 1, tmp);
tmp.pop_back();
}
}
vector<string> letterCombinations(string digits) {
int len = digits.size();
mp[2] = "abc";
mp[3] = "def";
mp[4] = "ghi";
mp[5] = "jkl";
mp[6] = "mno";
mp[7] = "pqrs";
mp[8] = "tuv";
mp[9] = "wxyz";
if (!len) return ans; //不加这个的话,ans有一个空串,跟什么都没有不一样!!!
string tmp;
dfs(digits, len, 0, tmp);
return ans;
}
};