[Leetcode] Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping:
'A' -> 1
'B' -> 2
...
'Z' -> 26
Given an encoded message containing digits, determine the total number of ways to decode it.
For example,
Given encoded message "12", it could be decoded as "AB" (1 2) or "L" (12).
The number of ways decoding "12" is 2.
Solution:
Use Dynamic Programming method to solve the problem, for positition i, we can get that
if substring(i-1,i) is a valid string, the number of ways decoding from the beginning to position i equals the number of ways decoding from the beginning to position i-1;
if substring(i-2,i) is a valid string, the number of ways decoding from the beginning to position i equals the number of ways decoding from the beginning to position i-2;
public class Solution {
public int numDecodings(String s) {
if(s==null||s.length()==0)
return 0;
int[] dp=new int[s.length()+1];
dp[0]=1;
if(isValid(s.substring(0,1))){
dp[1]=1;
}else{
dp[1]=0;
}
for(int i=2;i<=s.length();++i){
if(isValid(s.substring(i-1, i)))
dp[i]=dp[i-1];
if(isValid(s.substring(i-2, i))){
dp[i]+=dp[i-2];
}
}
return dp[s.length()];
}
private boolean isValid(String s) {
// TODO Auto-generated method stub
int N=s.length();
if(N==1){
return(s.charAt(0)>='1'&&s.charAt(0)<='9');
}else if(N==2){
return((s.charAt(0)=='1'&&s.charAt(1)>='0'&&s.charAt(1)<='9')||
(s.charAt(0)=='2'&&s.charAt(1)>='0'&&s.charAt(1)<='6'));
}
return false;
}
}
[Leetcode] Decode Ways的更多相关文章
- [LeetCode] Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- [LeetCode] Decode Ways II 解码方法之二
A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...
- LeetCode:Decode Ways 解题报告
Decode WaysA message containing letters from A-Z is being encoded to numbers using the following map ...
- [leetcode]Decode Ways @ Python
原题地址:https://oj.leetcode.com/problems/decode-ways/ 题意: A message containing letters from A-Z is bein ...
- [LeetCode] Decode Ways(DP)
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- [LeetCode] Decode Ways 解题思路
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- [LeetCode] Decode Ways [33]
题目 A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A ...
- [LeetCode] decode ways 解码方式
A message containing letters fromA-Zis being encoded to numbers using the following mapping: 'A' -&g ...
- [LeetCode] Decode Ways 解码方法个数、动态规划
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
随机推荐
- (转)Javascript本地存储小结
转自:https://i.cnblogs.com/EditPosts.aspx?opt=1 以下是原文: 1. 各种存储方案的简单对比 Cookies:浏览器均支持,容量为4KB UserData:仅 ...
- 开发unity DLL插件
最近开发一款设备的SDK,想着要开发unity版本,怎么做呢?首先想到的就是在外部编写相关的驱动程序然后集成成几个dll作为unity的SDK使用了.So,我就开始了unity外部插件的研究之旅了. ...
- Dapper.Net 应用
Dapper应用 1.Dapper是什么 Dapper是一款轻量级ORM工具.如果你在小的项目中,使用Entity Framework.NHibernate 来处理大数据访问及关系映射,未免有点杀鸡用 ...
- 14个技巧助你适配 iOS10
1.Notification(通知) 自从 Notification 被引入之后,苹果就不断的更新优化,但这些更新优化只是小打小闹,直至现在iOS 10开始真正的进行大改重构,这让开发者也体会到 Us ...
- 在ie浏览器,360浏览器下,margin:0 auto;不居中的原因
转自 http://blog.sina.com.cn/s/blog_6eef6bf60100nn4m.html margin:0 auto:不居中可能有以下两个的原因 没有设置宽度 看看上面的代码,根 ...
- 超全的web开发工具和资源
首页 新闻 产品 地图 动态 城市 帮助 论坛 关于 登录 注册 · 不忘初心,继续前进,环境云V2接口正式上线 · 环境云测点地图全新改版 · 祝福各位环境云用户中秋快乐! 平台信息 培训互动 ...
- 详细解读Android中的搜索框—— SearchView
以前总是自己写的 今天看看别人做的 本篇讲的是如何用searchView实现搜索框,其实原理和之前的没啥差别,也算是个复习吧. 一.Manifest.xml 这里我用一个activity进行信息的输入 ...
- 两个int的和判断溢出
long a,b; cin>>a>>b; long i; i = a+b; if((i^a)<0 && (i^b)<0) cout<<& ...
- JS中的call()和apply()方法
1.方法定义 call方法: 语法:call([thisObj[,arg1[, arg2[, [,.argN]]]]]) 定义:调用一个对象的一个方法,以另一个对象替换当前对象. 说明: call ...
- linux下添加mysql用户并授权
-------------------暂停mysqlservice mysqld stop 忘记密码修改流程1,修改mysql配置文件 /etc/my.cnf,在[mysqld]下添加一行skip-g ...