LeetCode #17 电话号码的字母组合
2020-02-09 本文已影响0人
HU兔兔
class Solution {
public:
vector<string> letterCombinations(string digits) {
map<int,string> phone={{2,"abc"},
{3,"def"},
{4,"ghi"},
{5,"jkl"},
{6,"mno"},
{7,"pqrs"},
{8,"tuv"},
{9,"wxyz"}};
vector<string> ans;
string s;
int i,m,j,flag;
vector<int> nums(digits.size());
if(digits.size()==0){
return ans;
}
for(i=0;i<digits.size();i++){
nums[i]=digits[i]-'0';
}
vector<int> n(digits.size(),0);
m=phone[nums[0]].size();
while(n[0]<m){
s="";
for(j=0;j<n.size();j++){
s.push_back(phone[nums[j]][n[j]]);
}
ans.push_back(s);
flag=1;
j=n.size()-1;
while(flag&&j>=0){
n[j]+=flag;
if(n[j]==phone[nums[j]].size()&&j!=0){
n[j]=0;
flag=1;
}
else{
flag=0;
}
j--;
}
}
return ans;
}
};