Java solution without dp array.


  • 0
    M
        public int numDecodings(String s) {
            if (s == null || s.length() == 0) return 0;
            
            int two = 0, one = 1;
            for (int i = 0; i < s.length(); i++) {
                int temp = 0;
                if (s.charAt(i) == '0') {
                    temp = 0;
                } else {
                    temp += one;
                }
                
                if (i > 0 && (s.charAt(i-1) == '1' || (s.charAt(i-1) == '2' && s.charAt(i) <= '6'))) {
                    temp += two;
                }
                
                two = one;
                one = temp;
            }
            return one;
        }
    

Log in to reply
 

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