首页 > 其他分享 >Letter Combinations of a Phone Number 手机的字符表示

Letter Combinations of a Phone Number 手机的字符表示

时间:2023-02-20 16:36:10浏览次数:42  
标签:return cur res Number len char Phone Letter string


Letter Combinations of a Phone Number


Given a digit string, return all possible letter combinations that the number could represent.

A mapping of digit to letters (just like on the telephone buttons) is given below.

Input:Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.

class Solution {
public:
char letter[10][5]={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
int len;
vector<string> res;

vector<string> letterCombinations(string digits) {
len=digits.size();
if(digits=="")
return res;
char *c=new char[len+1];
c[len]='\0';
res.clear();
getResult(digits,0,c);
return res;
}

void getResult(string num,int cur,char *c)
{
if(cur==len)
{
res.push_back(c);
return ;
}
char *s=&(letter[ num[cur]-'0' ][0]);
while(*s!='\0')
{
c[cur]=*s;
getResult(num,cur+1,c);
s++;
}
}
};

标签:return,cur,res,Number,len,char,Phone,Letter,string
From: https://blog.51cto.com/u_1382267/6068693

相关文章