Decode Ways

来源:互联网 发布:java calendar. Date 编辑:程序博客网 时间:2024/06/06 14:16
//http://www.cnblogs.com/springfor/p/3896162.html


public class Solution {
    public int numDecodings(String s) {
        if (s==null||s.length()==0||s == "0")
            return 0;
            
        int [] dp = new int[s.length()+1];
        dp[0] = 1;
        
        if(helper(s.substring(0,1))) 
            dp[1]=1;
        else dp[1]=0;
        
        for(int i=2; i<=s.length();i++){
            if(helper(s.substring(i-1,i)))
                dp[i] += dp[i-1];
            if(helper(s.substring(i-2,i)))
                dp[i] += dp[i-2];
        }
        
        return dp[s.length()];
    }
    
    public boolean helper(String s){
        //if (s.charAt(0)=='0') return false;
        int num = Integer.parseInt(s);
        
        return num>0 && num <27;
    }
        
        
    
}
0 0
原创粉丝点击