Just like system of carry! different from the recursive solution...


  • 0
    S
    vector<string> letterCombinations(string digits) {
        
        vector<string> res;
        int size=digits.size();
        if(size<1)
        return res;
        vector<string> temp={" ","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
        string curstr=digits;
        vector<int> curnum(size,0);
        int totalnum=1;
        for(int i=0;i<size;++i)
        {
            curnum[i]=digits[i]-'0';
            if(curnum[i]==1)
            continue;
            totalnum=totalnum*temp[curnum[i]].size();
        }
        vector<int> digit(size,0);
        for(int i=0;i<totalnum;++i)
        {
            int jinwei=1;
            for(int j=size-1;j>=0;--j)
            {
                if(curnum[j]!=1)
                curstr[j]=temp[curnum[j]][digit[j]];
                digit[j]+=jinwei;
                if(digit[j]>=temp[curnum[j]].size())
                {
                    digit[j]=digit[j]-temp[curnum[j]].size();
                    jinwei=1;
                }
                else
                jinwei=0;
            }
            res.push_back(curstr);
        }
        return res;
        
        
        
    }

Log in to reply
 

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