Decode Ways

A message containing letters from A-Z is 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 "12" is 2.

class Solution {
public:
     int numDecodings( string s) 
    {
         int size=s.length();
         if(size== 0return  0;
        
         int total[size];
         if(s[ 0]>= ' 1 ' && s[ 0]<= ' 9 ') total[ 0]= 1else total[ 0]= 0;
         if(size< 2return total[size- 1];
        
        total[ 1]= 0;
         if(total[ 0]> 0 && s[ 1]>= ' 1 ' && s[ 1]<= ' 9 ') total[ 1]= 1;
        
         if( 10*(s[ 0]- ' 0 ')+s[ 1]- ' 0 '>= 10 &&  10*(s[ 0]- ' 0 ')+s[ 1]- ' 0 '<= 26) total[ 1]++;
        
         for( int i= 2;i<size;i++)
        {
            total[i]= 0;
             if(total[i- 2]> 0 &&  10*(s[i- 1]- ' 0 ')+s[i]- ' 0 '>= 10 &&  10*(s[i- 1]- ' 0 ')+s[i]- ' 0 '<= 26
                total[i]+=total[i- 2];
             if(total[i- 1]> 0 && s[i]> ' 0 ' && s[i]<= ' 9 ') total[i]+=total[i- 1];
        }
         return total[size- 1];
    }
};  

你可能感兴趣的:(decode)