当前位置:网站首页>[early spring 2022] [leetcode] 91 Decoding method

[early spring 2022] [leetcode] 91 Decoding method

2022-06-09 06:03:00 Well of

A typical topic , Spell words
Note that the two states add up , It's not just +1, And pay attention to the problem of subscript

class Solution {
    
    public int numDecodings(String s) {
    
        
        int[] dp = new int[s.length()+1];
        dp[0] = 1;
        for(int i=1; i<=s.length(); i++){
    
            if(s.charAt(i-1)!='0') dp[i] = dp[i-1];
            if(i>1&&s.charAt(i-2)!='0'&&((s.charAt(i-2)-'0')*10+(s.charAt(i-1)-'0')<=26)){
    
                dp[i] += dp[i-2];
            }
        }
        return dp[s.length()];
    }
}
原网站

版权声明
本文为[Well of]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090558082213.html