leetcode91.解码方法

 

 


 

突然想明白这是动态规划

class Solution {
public:
    int numDecodings(string s) {
        int res[100];
        memset(res,0,sizeof(res));
        if(s[0]=='0') return 0;
        res[0]=1;
        if(s.length()==1) return 1;
        if(atoi(s.substr(0,2).c_str())<=26) res[1]+=1;
        if(s[1]!='0') res[1]+=1;
        int i;
        for(i=2;i<s.length();i++){
            if(s[i]!='0') res[i]+=res[i-1];
            if(atoi(s.substr(i-1,2).c_str())<=26 && s[i-1]!='0') res[i]+=res[i-2];
        }
        for(i=0;i<s.length();i++) cout<<res[i]<<" ";
        return res[s.length()-1];
    }
};

  

 

posted @ 2021-04-22 13:29  vdk  阅读(44)  评论(0)    收藏  举报