LeetCode 17 Letter Combinations of a Phone Number

题意:

给出数字串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;
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值