A message containing letters from was A-Z being encoded to numbers using the following mapping:
' A '-1 ' B '-2 ... ' Z '-26
Given an encoded message containing digits, determine the total number of ways to decode it.
For example,
Given encoded message "12" , it could be decoded as "AB" (1 2) or "L" (12).
The number of ways decoding is "12" 2.
1 intNumdecodings (strings)2 {3 intn =s.size (), ret;4 if(N <1|| s[0] =='0')5 return 0;6 7 int*ways =New int[n +1], I;8ways[0] =1;9ways[1] =1;Ten for(i =1; I < n; i++) One { A if(S[i] = ='0') - { - if(S[i-1] =='0'|| S[i-1] >'2') the return 0; - Else -Ways[i +1] = ways[i-1]; - } + Else if(S[i] <='6') - { + if(S[i-1] =='0'|| S[i-1] >'2') AWays[i +1] =Ways[i]; at Else -Ways[i +1] = Ways[i] + ways[i-1]; - } - Else - { - if(S[i-1] =='1') inWays[i +1] = Ways[i] + ways[i-1]; - Else toWays[i +1] =Ways[i]; + } - } the *RET =Ways[n]; $ delete[] ways;Panax Notoginseng returnret; -}
Leetcode. Decode Ways