2ms c++ easy understand backtrace


  • 0
    Y
        void letterCombinations(string digits, int s, int e, vector<string> &v, string str, string phone_letter[])
        {
            if(s==e)
            {
                v.push_back(str);
                return;
            }
            for(int i = 0; i < phone_letter[digits[s]-'2'].size();++i)
            {
                str = str + phone_letter[digits[s]-'2'][i];
                letterCombinations(digits, s+1, e, v, str, phone_letter);
                str.pop_back();
            }
        }
        vector<string> letterCombinations(string digits) {
            vector<string> ret;
            if(digits.empty())
                return ret;
            string str = "";
            string phone_letter[8] = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
            letterCombinations(digits, 0, digits.size(), ret, str, phone_letter);
            return ret;
        }

Log in to reply
 

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