Poor recursive cpp solution


  • -1
    D

    only 25 testcases, finish 0ms

    class Solution {
            vector<vector<string>> dictionary ={{},{},{"a","b","c"},{"d","e","f"},{"g","h","i"},{"j","k","l"},{"m","n","o"},{"p","q","r","s"},{"t","u","v"},{"w","x","y","z"},}; 
        public:
            vector<string> letterCombinations(string digits) {
                vector<string> res;
                int len = digits.length();
                if(len == 1)
                    return dictionary[std::stoi(digits)];
                else if(len == 0)
                    return dictionary[0];
                else
                {
                    vector<string> pre = letterCombinations(digits.substr(0,len - 1));
                    for(std::vector<string>::iterator itor = pre.begin(); itor != pre.end(); ++itor)
                    {
                        vector<string> cur = dictionary[std::stoi(digits.substr(len - 1,1))];
                        for(std::vector<string>::iterator curchar = cur.begin(); curchar != cur.end(); ++curchar)
                        {
                            res.push_back(*itor + *curchar);
                        }
                    }
                }
                return res;
            }
        };

Log in to reply
 

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