您的位置:首页 > 其它

LeetCode 17 Letter Combinations of a Phone Number

2017-01-24 14:37 369 查看
题意:

给出数字串s,输出按照9键键盘输入s时可能的所有字符串。

思路:

没思路……直接模拟过程就得了……

写switch好看点……吧……

代码:

class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> res;
if(!digits.size()){
return res;
}
res.push_back("");
for(int i = 0; i < digits.size(); ++i){
res = grow(res, digits[i]);
}
return res;
}
private:
vector<string> grow(vector<string> fath, char son){
vector<string> res;
switch(son){
case '1':{
res = fath;
break;
}
case '2':{
for(string s : fath){
res.push_back(s + 'a');
res.push_back(s + 'b');
res.push_back(s + 'c');
}
break;
}
case '3':{
for(string s : fath){
res.push_back(s + 'd');
res.push_back(s + 'e');
res.push_back(s + 'f');
}
break;
}
case '4':{
for(string s : fath){
res.push_back(s + 'g');
res.push_back(s + 'h');
res.push_back(s + 'i');
}
break;
}
case '5':{
for(string s : fath){
res.push_back(s + 'j');
res.push_back(s + 'k');
res.push_back(s + 'l');
}
break;
}
case '6':{
for(string s : fath){
res.push_back(s + 'm');
res.push_back(s + 'n');
res.push_back(s + 'o');
}
break;
}
case '7':{
for(string s : fath){
res.push_back(s + 'p');
res.push_back(s + 'q');
res.push_back(s + 'r');
res.push_back(s + 's');
}
break;
}
case '8':{
for(string s : fath){
res.push_back(s + 't');
res.push_back(s + 'u');
res.push_back(s + 'v');
}
break;
}
case '9':{
for(string s : fath){
res.push_back(s + 'w');
res.push_back(s + 'x');
res.push_back(s + 'y');
res.push_back(s + 'z');
}
break;
}
case '0':{
for(string s : fath){
res.push_back(s + ' ');
}
break;
}
}
return res;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: