C# dp


  • 0
    H
    public int NumDecodings(string s) {
            if (string.IsNullOrEmpty(s) || s[0] == '0') return 0;
            int d1 = 1, d2 = 1;
            for(int i = 1; i < s.Length; i++) {
                if (s[i] == '0') d1 = 0;
                if (s[i - 1] == '1' || (s[i - 1] == '2' && s[i] <= '6')) {
                    int t = d2;
                    d2 = d1;
                    d1 += t; 
                } else d2 = d1;
            }
            
            return d1;
        }
    

Log in to reply
 

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