LeetCode (17)Letter Combinations of a Phone Number

31 篇文章 0 订阅

(17)Letter Combinations of a Phone Number

题目:通过所给字符串,返回按下数字可能返回的字符串组合,数字和字符的关系如同手机键盘一样。

例子:

输入:字符串为“23”。
输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]。

首先想到的就是队列,不停地通过队列的每一组字符串相加获得下一组字符串,然后再把下一组字符串压入队列中,这样能够实现整个字符串的所有可能性的罗列。

当然在C++中vector有着类似队列和栈的功能,每一次拿出v.back()做基础字符串,然后v.pop_back(),直到v.empty(),将做好的新字符串放到v_temp中,最后v=v_temp,慢慢将所有的字符串处理完成就可以了。

下面是代码:

class Solution {
public:
    vector<string> letterCombinations(string digits) {
        int len_str = digits.size();
        char c;
        string word[10] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
        int i,j;
        vector<string> s;
        int len = 0;
        for(i = 0; i < len_str; i ++){
            c = digits[i];
            if(c == '1'){
            }
            else if(c == '0'){
                vector<string> temp_v;
                if(len == 0){
                    temp_v.push_back(" ");
                    s = temp_v;
                    len ++;
                }
                else{
                    while(!s.empty()){
                        string str_temp = "";
                        str_temp += s.back();
                        s.pop_back();
                        str_temp += " ";
                        temp_v.push_back(str_temp);
                    }
                    s = temp_v;
                }
            }
            else if(c == '2'||c == '3'||c == '4'||c == '5'||c == '6'||c == '8'){
                vector<string> temp_v;
                if(len == 0){
                    string str_temp = "";
                    string s1 = str_temp;
                    s1.append(word[c - '0'].substr(0,1));
                    temp_v.push_back(s1);
                    string s2 = str_temp;
                    s2.append(word[c - '0'].substr(1,1));
                    temp_v.push_back(s2);
                    string s3 = str_temp;
                    s3.append(word[c - '0'].substr(2,1));
                    temp_v.push_back(s3);
                    s = temp_v;
                    len ++;
                }
                else{
                    while(!s.empty()){
                        string str_temp = "";
                        str_temp += s.back();
                        s.pop_back();
                        string s1 = str_temp;
                        s1.append(word[c - '0'].substr(0,1));
                        temp_v.push_back(s1);
                        string s2 = str_temp;
                        s2.append(word[c - '0'].substr(1,1));
                        temp_v.push_back(s2);
                        string s3 = str_temp;
                        s3.append(word[c - '0'].substr(2,1));
                        temp_v.push_back(s3);
                    }
                    s = temp_v;
                }
            }
            else if(c == '7'||c == '9'){
                vector<string> temp_v;
                if(len == 0){
                    string str_temp = "";
                    string s1 = str_temp;
                    s1.append(word[c - '0'].substr(0,1));
                    temp_v.push_back(s1);
                    string s2 = str_temp;
                    s2.append(word[c - '0'].substr(1,1));
                    temp_v.push_back(s2);
                    string s3 = str_temp;
                    s3.append(word[c - '0'].substr(2,1));
                    temp_v.push_back(s3);
                    string s4 = str_temp;
                    s4.append(word[c - '0'].substr(3,1));
                    temp_v.push_back(s4);
                    s = temp_v;
                    len ++;
                }
                else{
                    while(!s.empty()){
                        string str_temp = "";
                        str_temp += s.back();
                        s.pop_back();
                        string s1 = str_temp;
                        s1.append(word[c - '0'].substr(0,1));
                        temp_v.push_back(s1);
                        string s2 = str_temp;
                        s2.append(word[c - '0'].substr(1,1));
                        temp_v.push_back(s2);
                        string s3 = str_temp;
                        s3.append(word[c - '0'].substr(2,1));
                        temp_v.push_back(s3);
                        string s4 = str_temp;
                        s4.append(word[c - '0'].substr(3,1));
                        temp_v.push_back(s4);
                    }
                    s = temp_v;
                }
            }
        }
        return s;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值