Java 1ms very easy AC solution


  • 0
    V
    public int numDecodings(String s) {
        if(s.length() == 0) return 0;
        int pre = 27, digit, answer = 0, first = 1, second = 1;
        for(int i = s.length()-1; i >= 0; i--){
            digit = s.charAt(i) - '0';
            if(digit == 0) answer = 0;
            else answer = first + (digit*10 + pre < 27 ? second : 0);
            second = first; first = answer; pre = digit;
        }
        return answer;
    }

Log in to reply
 

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