leetcode- Letter Combinations of a Phone Number

本文介绍了一种解决电话号码对应字母组合问题的算法实现,通过递增方式生成所有可能的字母组合。针对输入的数字字符串,算法能够返回所有可能的字母组合,详细解释了如何映射数字到字母,并提供了具体的代码实现。

Question:
Given a digit string, return all possible letter combinations that the number could represent.

A mapping of digit to letters (just like on the telephone buttons) is given below.

Input:Digit string “23”
Output: [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”].

Solution:

class Solution {
public:
    vector<string> letterCombinations(string digits) {
        vector<string> res;
        int len = digits.size();
        if(len <= 0) return res;
        int first = 0;
        while(digits[first] <'2' || digits[first] > '9') first++;
        int num = (digits[first] - '2') * 3;
        if(digits[first] <= '7'){
            stringstream ss;
            string s;
            ss<<(char)('a'+num);
            ss>>s;
            res.push_back(s);
            ss.clear();
            ss<<(char)('b'+num);
            ss>>s;
            res.push_back(s);
            ss.clear();
            ss<<(char)('c'+num);
            ss>>s;
            res.push_back(s);
        }
        if(digits[first] == '7'){
            res.push_back("s");
        }
        else if(digits[first] == '8'){
            res.push_back("t");
            res.push_back("u");
            res.push_back("v");
        }
        else if(digits[first] == '9'){
            res.push_back("w");
            res.push_back("x");
            res.push_back("y");
            res.push_back("z");
        }
        for(int  i = 1 ; i < len ;i++){
            if(digits[i] < '2' || digits[i] > '9') continue;
            int num = (digits[i] - '2') * 3;
            string tmp = res.front();
            int nowlen = tmp.size();
            while(tmp.size() == nowlen){
                res.erase(res.begin());
                if(digits[i] <= '7'){
                    res.push_back(tmp+(char)(97 + num));
                    res.push_back(tmp+(char)(98 + num));
                    res.push_back(tmp+(char)(99 + num));
                }
                if(digits[i] == '7'){
                    res.push_back(tmp+"s");
                }
                else if(digits[i] == '8'){
                res.push_back(tmp+"t");
                res.push_back(tmp+"u");
                res.push_back(tmp+"v");
                }
                else if(digits[i] == '9'){
                    res.push_back(tmp+"w");
                    res.push_back(tmp+"x");
                    res.push_back(tmp+"y");
                    res.push_back(tmp+"z");
                }
                tmp = res.front();
            }

        }
        return res;
    }
};

Attention:

  1. methods of char to string:

“”+ char //wrong
char+ “”//wrong
stringstream ss;string s;//right
stringstream ss;ss.str()//wrong
2. from key ‘7’ ,it make changes. not ‘9’
3. vector has’t function of pop_front(), but list have.

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值