标签:bin tps class ring problems ems letter phone tco
【题解】
用回溯法搞一下。
一搞就有~
注意输入空串的时候别返回那个空串。。
【代码】
class Solution {
public:
string dic[10];
vector<string> ans;
void dfs(string &digits,int idx,string cur){
if (idx==(int)digits.size()){
ans.push_back(cur);
return;
}
int len = dic[(int)(digits[idx]-'0')].size();
for (int i = 0;i < len;i++){
dfs(digits,idx+1,cur+dic[(int)(digits[idx]-'0')][i]);
}
}
vector<string> letterCombinations(string digits) {
ans.clear();
if (digits.size()==0) return ans;
dic[2] = "abc";dic[3] = "def";dic[4] = "ghi";dic[5]="jkl";
dic[6]="mno";dic[7] = "pqrs";dic[8] = "tuv";dic[9] = "wxyz";
dfs(digits,0,"");
return ans;
}
};
标签:bin tps class ring problems ems letter phone tco
原文地址:https://www.cnblogs.com/AWCXV/p/11809847.html