share my java dp solution


  • 0
    M
    public class Solution {
        public int numDecodings(String s) {
            if(s.length() == 0) return 0;
            int[] dp = new int[s.length()+1];
            dp[0] = 1;
            for(int i = 0; i < s.length(); i ++) {
                char c = s.charAt(i);
                if(c != '0') dp[i+1] += dp[i];
                if(i == s.length() - 1) continue;
                if(c == '1' || (c =='2' && s.charAt(i+1) <= '6')) dp[i+2] += dp[i];
            }
            return dp[s.length()];
        }
    }
    

Log in to reply
 

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