My simple c++ solution


  • 0
    K
    class Solution {
    public:
    int numDecodings(string s) {
        int len=s.length();
        if(len==0 ||s[0]=='0')return 0;
        vector<int>dp(len+1, 0);
        dp[0]=1;
        dp[1]=1;
        for(int i=2; i <= len; i++){
            if(s[i-1]!='0')dp[i]=dp[i-1];
            if(s[i-2]!='0'&& stoi(s.substr(i-2, 2)) <=26  && stoi(s.substr(i-2, 2)) > 0)dp[i]+=dp[i-2];
            if(dp[i]==0)return 0;
            
        }
        return dp[len];
    }
    };

Log in to reply
 

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