美文网首页
17.电话号码的字母组合

17.电话号码的字母组合

作者: Gunther17 | 来源:发表于2018-11-08 09:25 被阅读12次

    给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

    电话号码

    示例:
    输入:"23"
    输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

    如何评价 C++ 11 auto 关键字

    三个for循环暴力字典 c++ code :AC 100%

    class Solution {
    public:
        vector<string> letterCombinations(string digits) {
            //题目说的2-9
            vector<string>s1, s2;
            map<char, vector<string>>mp;
            mp['2'] = { "a", "b", "c" };
            mp['3'] = { "d", "e", "f" };
            mp['4'] = { "g", "h", "i" };
            mp['5'] = { "j", "k", "l" };
            mp['6'] = { "m", "n", "o" };
            mp['7'] = { "p", "q", "r", "s" };
            mp['8'] = { "t", "u", "v" };
            mp['9'] = { "w", "x", "y", "z" };
            for (auto &ch : digits)
            {
                int size = s1.size();
                auto &v = mp[ch];
                for (auto &s : v)
                {
                    for (int i = 0; i < size; i++)
                    {
                        string temp = s1[i] + s;
                        s2.push_back(temp);
                    }
                    if (size == 0)s2.push_back(s);
                }
                swap(s1, s2);
                s2.clear();
    
            }
            return s1;
    
        }
    };
    

    相关文章

      网友评论

          本文标题:17.电话号码的字母组合

          本文链接:https://www.haomeiwen.com/subject/vdipxqtx.html