javascript + DP


  • 0
    W
     * @param {string} s
     * @return {number}
     */
    var numDecodings = function(s) {
        var dp =[];
        var arr = s.split('');
        var n = arr.length;
        if(0 == n) return 0;
        
        if(arr[0] == 0)return 0;
        
        dp[n] = 1;
        dp[n-1] = (arr[n-1] != 0) ? 1 : 0;
        
        for(var i= n-2; i>=0; i--){
            if( arr[i] == 0 ){
                dp[i] = 0;
                continue;
            }
            else{
                dp[i] = (parseInt(arr[i] +arr[i+1]) <= 26) ? dp[i + 1] + dp[i + 2] : dp[i + 1];
            }
        }
        
        return dp[0];
    };
    

Log in to reply
 

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