My C++11 code ~~~~


  • 0
    S
    /************************
     * author : s2003zy
     * weibo  : weibo.com/songzy982
     ***********************/
    class Solution {
    public:
        vector<string> letterCombinations(string digits) {
            if (digits == "") return vector<string>{};
            unordered_map<char, string> hash{{'2', "abc"}, {'3', "def"}, 
                                            {'4', "ghi"}, {'5', "jkl"}, {'6', "mno"}, 
                                            {'7', "pqrs"}, {'8', "tuv"}, {'9', "wxyz"}};
            vector<string> res[2];
            res[0].push_back("");
            bool last = 0;
            for (auto x : digits) {
                res[!last].clear();
                string str = hash[x];
                for (auto c : str) {
                    for (auto x : res[last]) {
                        res[!last].push_back( x + c);
                    }
                }
                last = !last;
            }
            return res[last];
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.