leetcode面试准备:Decode Ways
1 题目
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.
接口:public int numDecodings(String s);
2 思路
一维动态规划,偷懒了,照搬博文。
分析:需要注意的是,如果序列中有不能匹配的0,那么解码方法是0,比如序列012 、100(第二个0可以和1组成10,第三个0不能匹配)。
- 递归的解法很容易,但是大集合会超时。转换成动态规划的方法,假设dp[i]表示序列s[0...i-1]的解码数目
动态规划方程如下:
- 初始条件:dp[0] = 1, dp[1] = (s[0] == '0') ? 0 : 1
- dp[i] = ( s[i-1] == 0 ? 0 : dp[i-1] ) + ( s[i-2,i-1]可以表示字母 ? dp[i-2] : 0 ), 其中第一个分量是把s[0...i-1]末尾一个数字当做一个字母来考虑,第二个分量是把s[0...i-1]末尾两个数字当做一个字母来考虑
复杂度: Time O(n); Space O(n)
3 代码
public int numDecodings(String s) {
// 1.初始化
final int len = s.length();
if (len == 0)
return 0;
int[] dp = new int[len + 1];
dp[0] = 1;
if (s.charAt(0) != '0')
dp[1] = 1;
else
dp[1] = 0;
// 2.一维DP方程
for (int i = 2; i <= len; i++) {
if (s.charAt(i - 1) != '0')
dp[i] = dp[i - 1];
else
dp[i] = 0;
if (s.charAt(i - 2) == '1'
|| (s.charAt(i - 2) == '2' && s.charAt(i - 1) <= '6'))
dp[i] += dp[i - 2];
}
return dp[len];
}
4 总结
写递归的解法
5 参考
leetcode面试准备:Decode Ways的更多相关文章
- 【LeetCode】91. Decode Ways 解题报告(Python)
[LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- 【LeetCode】091. Decode Ways
题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...
- 【一天一道LeetCode】#91. Decode Ways
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 A messa ...
- 【LeetCode】91. Decode Ways
题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...
- [leetcode DP]91. Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- LeetCode OJ:Decode Ways(解码方法)
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@ [91] Decode Ways (Dynamic Programming)
https://leetcode.com/problems/decode-ways/ A message containing letters from A-Z is being encoded to ...
- [LeetCode] Decode Ways II 解码方法之二
A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...
随机推荐
- 曾经感动过我们的文字 今天是否还有印象?——v1
①人最宝贵的东西是生命.生命对人来说只有一次.因此,人的一生应当这样度过:当一个人回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧;这样,在他临死的时候,能够说,我把整个生命和全部精力都献给了人生 ...
- 基础之 window-self-top-opener
今天我都在怀疑,很多项目还用不用iframe这个框架做页面布局. 如果你有兴趣想告诉我,请给我留言. 一. 说明 注:这里top和window.top等价,window是可以省略的,有得情况下不允许省 ...
- 消息处理之EventBus ——使用篇
以前的几篇文章简单的介绍了一下UI线程和子线程之间的线程通信利器Handler,以及顺便介绍了一下SyncTask和HeadlerThread.这里介绍另一线程通信利器EventBus. EventB ...
- 获取SqlServer当前链接数
1.提供有关 Microsoft SQL Server 数据库引擎实例中的当前用户.会话和进程的信息,显示所有session sp_who 2.针对 SQL Server 上的每个经过身份验证的会话返 ...
- js setInterval和clearInterval 的使用
setInterval(函数名, 时间); 函数名:不需要加括号: 时间:单位是毫秒: 例子: var inter= setInterval(searchTasksByCnd, 10 * 100 ...
- 判断ios是app第一次启动
首次运行的应用程序加入一些help 或者 宣传动画 现在变的很重要了. 一个有用的例子是发送一个分析实例.这可能是一个很好的方法来确定有多少人下载实用应用程序.有人会说:“但是,嘿,苹果AppStor ...
- iOS 十六进制的相加取反
ios中将NSstring字符串转换成char类型 NSString *string = [NSString stringWithFormat:@"5D"]; const char ...
- 怎样在win7上远程连接linux系统
window操作系统的电脑 一台安装了linux系统的服务器 putty.exe小软件 方法/步骤 在前面的环境和软件都有的情况下,双击putty.exe软件,如下图: 在软件界面中的:Hos ...
- 转:探讨android更新UI的几种方法
本文转自:http://www.cnblogs.com/wenjiang/p/3180324.html 作为IT新手,总以为只要有时间,有精力,什么东西都能做出来.这种念头我也有过,但很快就熄灭了,因 ...
- Linux VPS使用百度网盘API上传备份文件
最近百度网盘将空间升级到了永久1TB,鉴于百度的实力用做数据备份空间不错,不过百度网盘没有Linux下的客户端,上传管理文件需通过百度开放云平台访问PCS资源的系列接口. 1.首先加入百度开发者:ht ...