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;
}
};
给出数字串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;
}
};
相关文章推荐
- 【leetcode】String——Letter Combinations of a Phone Number (17)
- LeetCode17——Letter Combinations of a Phone Number(手机几个按键对应的字母(符号)组合)
- LeetCode 17 Letter Combinations of a Phone Number 递归与DFS的使用
- LeetCode (17)Letter Combinations of a Phone Number
- leetcode_17题——Letter Combinations of a Phone Number(简单题)
- LeetCode (17)Letter Combinations of a Phone Number
- leetcode[17]Letter Combinations of a Phone Number
- LeetCode 17 Letter Combinations of a Phone Number (电话号码字符组合)
- Leetcode 17 Letter Combinations of a Phone Number
- LeetCode 17 - Letter Combinations of a Phone Number
- Leetcode-17: Letter Combinations of a Phone Number
- LeetCode 17 Letter Combinations of a Phone Number 递归与DFS的使用
- leetcode 17 -- Letter Combinations of a Phone Number
- leetcode 题解 || Letter Combinations of a Phone Number 问题
- LeetCode刷题笔录Letter Combinations of a Phone Number
- LeetCode | #17 Letter Combinations of a Phone Number
- [LeetCode 17] Letter Combinations of a Phone Number
- LeetCode算法题之Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- [LeetCode]17 Letter Combinations of a Phone Number