Most clear C++ DP solution~


  • -1
    J

    int numDecodings(string s) {

        if(s.length()==0||s[0]=='0')
            return 0;
        int pre=1,prepre=1;
        for(int i=1;i<s.length();i++){
            int sum=0;
            if(s[i]!='0')
                sum+=pre;
            int t3=s[i]-'0'+10*(s[i-1]-'0');
            if(t3>=10&&t3<=26)
                sum+=prepre;
            prepre=pre;
            pre=sum;
        }
        return pre;
    

    }


Log in to reply
 

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