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

Jan 23 - Decode Ways; DP; Array;

时间:2016-01-24 15:28:51      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

public class Solution {
    public int numDecodings(String s) {
        int len = s.length();
        if(len == 0) return 0;
        int[] counts = new int[len];
        counts[0] = 0;
        for(int i = 0; i < len; i++){
            int flag = 0;
            if(i == 0) {
                if(s.charAt(i) != ‘0‘) counts[i] = 1;
                else counts[i] = 0;
                continue;
            }
            if(i == 1){
                int mes = Integer.parseInt(s.charAt(i-1)+""+s.charAt(i));
                if(mes >= 10 && mes <= 26) {
                   flag++;
                }
                if(s.charAt(i) != ‘0‘) flag++;
                if(flag == 2) counts[i] = counts[i-1]+1;
                if(flag == 1) counts[i] = counts[i-1];
                if(flag == 0) counts[i] = 0;
                continue;
            }
            
                int mes = Integer.parseInt(s.charAt(i-1)+""+s.charAt(i));
                if(mes >= 10 && mes <= 26) {
                    flag++;
                    counts[i] = counts[i-2];
                }
                if(s.charAt(i) != ‘0‘) {
                    flag++;
                    counts[i] = counts[i-1];
                }
                if(flag == 2) counts[i] = counts[i-2]+counts[i-1];
            
        }
        
        return counts[len-1];
    }
}

 

Jan 23 - Decode Ways; DP; Array;

标签:

原文地址:http://www.cnblogs.com/5683yue/p/5155091.html

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