当前位置: 移动技术网 > IT编程>开发语言>C/C++ > Letter Combinations of a Phone Number(C++)

Letter Combinations of a Phone Number(C++)

2018年10月05日  | 移动技术网IT编程  | 我要评论

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.

\

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();
}
}
}
};

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网