Easy solution to this question


  • 0
    N
    class Solution {
    public:
        int numDecodings(string s) {
            //动态规划
           if(s.size()==0 || s[0]=='0') return 0;
           int last1 = 1,last2 = 1;
           for(int i = 1;i<s.size() && last1!=0;i++)
           {
               int cur = 0;
               if(s[i]!='0') cur+=last1;
               if(s[i-1]=='1' || (s[i-1]=='2' && s[i]<='6')) cur+=last2;
               last2 = last1;
               last1 = cur;//如果中间遇到了0 则直接返回0
           }
           return last1;
         }
    };

  • 0
    This post is deleted!

Log in to reply
 

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