Recursive clean solution 0ms in C++


  • 0
    private:
        int Len;
        string digit_map[8]{"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        void search(const string& digits, int pos, string path, vector<string>& v)
        {
            if(pos == Len) return ;
            string s = digit_map[digits[pos]-'2'];
            for(int i = 0; s[i]; ++i)
            {
                if(pos == Len-1) v.push_back(path+s[i]);
                else search(digits, pos+1, path+s[i], v);
            }
        }
    public:
        vector<string> letterCombinations(string digits) 
        {
            Len = digits.length();
            vector<string> v;
            search(digits, 0, "", v);
            return v;
        }
    };

  • 0
    S

    class Solution {
    public:
    char ph[10][4]={{'0','0','0','0',},{'0','0','0','0',},{'a','b','c','0'},{'d','e','f','0'},{'g','h','i','0'},{'j','k','l','0'},{'m','n','o','0'},{'p','q','r','s'},{'t','u','v','0'},{'w','x','y','z'}};
    vector<string> ans;
    void print(string digits,string st,int pos)
    {
    int i,l=digits.size();
    if(l==pos)
    {
    ans.push_back(st);
    return;
    }
    else
    {
    for(i=pos;i<l;i++)
    {
    int ch=digits[i]-'0';

                for(int j=0;j<4 && ph[ch][j]!='0';j++)
                {
                    //st=st+ph[ch][j];
                    print(digits,st+ph[ch][j],i+1);
                }
            }
            //return;
        }
     }
    vector<string> letterCombinations(string digits) {
        int l=digits.size();
        if(!l)
            return ans;
        print(digits,"",0);
        return ans;
    }
    

    };


  • 0

    @sarcasm-123 You really should format your code properly before anyone trying to help you, this is ....


Log in to reply
 

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