您的位置:首页 > 其它

Letter Combinations of a Phone Number

2013-09-03 16:32 363 查看
class Solution {/*Straightforward, and easy**/
private:
vector<string> rep;
vector<string> result;
void build(const string & digits, int digitIndex, string ss)
{
int  num = digits[digitIndex]-'0';
if (digitIndex + 1 == digits.length())
{
for(int buttonIndex = 0; buttonIndex < rep[num].length(); ++buttonIndex)
{
result.push_back(ss+rep[num][buttonIndex]);
}
return;
}
for(int buttonIndex = 0; buttonIndex < rep[num].length(); ++buttonIndex)
{
build(digits, digitIndex+1,ss+rep[num][buttonIndex]);
}
return;
}
public:
vector<string> letterCombinations(string digits) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (digits.empty()) return vector<string>(1,"");
result.clear();
rep.resize(10);
rep[2]="abc";
rep[3]="def";
rep[4]="ghi";
rep[5]="jkl";
rep[6]="mno";
rep[7]="pqrs";
rep[8]="tuv";
rep[9]="wxyz";
build(digits, 0, "");
return result;
}
};

 

DFS 题目
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: