JavaScript DP O(1)


  • 0
    J
    /**
     * @param {string} s
     * @return {number}
     */
    var numDecodings = function(s) {
        var i, cur = 0, prev = 1, prev2 = 0;
        for (i = s.length - 1; i >= 0; i--) {
            if (s[i] === '0') {
                cur = 0;
            } else {
                cur = prev;
                if (parseInt(s[i] + s[i + 1]) <= 26)
                    cur += prev2;
            }
            prev2 = prev; prev = cur;
        }
        return cur;
    };
    

Log in to reply
 

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