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;
}
}
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
- Decode Ways
- Decode Ways
- Decode Ways
- Decode Ways
- Decode Ways
- Decode Ways
- Decode Ways
- Decode Ways
- Decode Ways
- Decode Ways
- Decode Ways
- Decode Ways
- Decode Ways
- Decode ways
- Decode Ways
- Decode Ways
- Decode Ways
- Decode Ways
- 119_动态创建fragment笔记
- D3D9 effect研究及学习
- 图像处理之基于Otsu阈值二值化
- C语言知识点总结
- fstream的使用方法介绍
- Decode Ways
- Java—Timer和TimerTask详解(常用API)
- UVA1592 Database
- 如何在 Ubuntu 14.04 中安装 Winusb
- Uva 133 - The Dole Queue
- hdu 1679 The Unique MST 次小生成树 简单题
- [Leetcode 102 and 107, Easy] Binary Tree Level Order Traversal (I and II)
- 翻译 Secrets of the JavaScript Ninja (JavaScript忍者禁术)
- 浏览器标识趣事