DP Leetcode 091解码方法

地址

https://leetcode-cn.com/problems/decode-ways/

描述

在这里插入图片描述

思想

在这里插入图片描述

代码

class Solution {
    
    
public:
    int numDecodings(string s) {
    
    
        int n=s.length();
        vector<int> f(n+1);
        f[0]=1;
        s=' '+s;
        for(int i=1;i<=n;i++){
    
    
            if(s[i]>='1'&&s[i]<='9') f[i]+=f[i-1];
            if(i>1){
    
    
                int t=0;
                //计算两位数的大小假如是类似06这样的数,无法加入
                t=(s[i-1]-'0')*10+(s[i]-'0');
                if(t>=10&&t<=26) f[i]+=f[i-2];
            }
        }
        return f[n];
    }
};

猜你喜欢

转载自blog.csdn.net/qq_52934831/article/details/121863986