欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Letter Combinations of a Phone Number(C++)

程序员文章站 2022-07-01 18:26:39
given a string containing digits from2-9inclusive, return all possible letter combinations that the...

given a string containing digits from2-9inclusive, 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. note that 1 does not map to any letters.

Letter Combinations of a Phone Number(C++)

class solution {
public:
vector lettercombinations(string digits)
{
int n=digits.size();
vector ret;
if(n==0)
return ret;
string dict[]={"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
findall(digits,dict,0,"",ret);
return ret;
}
void findall(string digits,string dict[],int level,string out,vector &ret)
{
if(level==digits.size())
return ret.push_back(out);
else
{
string str=dict[digits[level]-'2'];
for(int i=0;i
{
out.push_back(str[i]);
findall(digits,dict,level+1,out,ret);
out.pop_back();
}
}
}
};