Java short code, 1 for loop, Dynamic Programming


  • 0
    public class Solution {
    public int numDecodings(String s) {
        int n = s.length(), r=0, p1=0, p2=1;
        for (int i=0; i<n; i++) {
            r = 0;
            if (i>0 && 10*(s.charAt(i-1)-'0')+s.charAt(i)-'0'>9 && 10*(s.charAt(i-1)-'0')+s.charAt(i)-'0'<27) r += p1;
            if (s.charAt(i)!='0') r += p2;
            p1 = p2;
            p2 = r;
        }
        return r;
    }
    

    }


Log in to reply
 

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