Share my beats 92% 1ms solution


  • 1
    S
    public int numDecodings(String s) {
        if (s.length() == 0) return 0;
        char[] c = s.toCharArray();
        int[] dp = new int[c.length+1];
        dp[0] = 1;
        for (int i = 0; i < c.length; i++) {
            if (c[i] == '0'){
                if(i==0 || (c[i-1] != '1' && c[i-1] != '2')) return 0;
                dp[i+1] = dp[i-1];
                continue;
            }
            dp[i+1] = dp[i];
            if (i > 0 && (c[i-1]-'0' == 1 && c[i]-'0' < 10 || c[i-1]-'0' == 2 && c[i]-'0' < 7))
    	    dp[i+1]+=dp[i-1];
        }
        return dp[c.length];
    }
    

Log in to reply
 

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