码迷,mamicode.com
首页 > 其他好文 > 详细

lintcode-512-解码方法

时间:2020-02-19 20:54:13      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:sage   解码   color   str   solution   res   ram   string   for   

public class Solution {
    /**
     * @param s: a string,  encoded message
     * @return: an integer, the number of ways decoding
     */
    public static int numDecodings(String s) {
        // write your code here
        if(s.length()==0)
            return 0;
        int[] dp = new int[s.length()+1];
        dp[0]=1;
        for(int i=1;i<=s.length();++i){
            int num2 = s.charAt(i-1)-‘0‘;
            if(num2>0&&num2<=9){
                dp[i]+=dp[i-1];
            }
            if(i-2>=0) {
                int num1 = s.charAt(i - 2) - ‘0‘;
                int num3 = num1 * 10 + num2;
                if (num3 >= 10 && num3 <= 26) {
                    dp[i] += dp[i - 2];
                }
            }
        }
        return dp[s.length()];
    }

    public static void main(String[] args) {
        int res = numDecodings("12");
        System.out.println(res);
    }
}

 

lintcode-512-解码方法

标签:sage   解码   color   str   solution   res   ram   string   for   

原文地址:https://www.cnblogs.com/t1314/p/12332916.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!