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: ' ...
随机推荐
- Android开发之隐藏Activity(活动)的标题
隐藏一个活动的标题(如您打算向用户显示状态更新时),可以使用requestWindowFeature()方法,传递Window.FEATURE_NO_TITLE常量来控制.实现如下: protecte ...
- Java基础知识强化之IO流笔记31:转换流出现的原因和格式
1. 由于字节流操作中文不是特别方便,所以Java就提供了转换流. 字符流 = 字节流 + 编码表 2. 编码表 由字符及其对应数值组成的一张表 常见的编码表: • ASCII/Unicode字符集 ...
- Caused by: java.lang.ClassNotFoundException: com/sun/tools/internal/xjc/api/XJC
Caused by: java.lang.ClassNotFoundException: com/sun/tools/internal/xjc/api/XJC 缺少com/sun/tools/inte ...
- 基于Memcache的分布式缓存系统详解
文章不是简单的的Ctrl C与V,而是一个字一个标点符号慢慢写出来的.我认为这才是是对读者的负责,本教程由技术爱好者成笑笑(博客:http://www.chengxiaoxiao.com/)写作完成. ...
- angularjs小知识
字符串和对象的转化 :angular.fromJson(jsonStr) 对象转字符串 :angular.toJson(obj) jsonStr:json字符串 obj:对象
- A题笔记(7)
No. 1468 已知三角形的三条边求面积:海伦公式 S=√[p(p-a)(p-b)(p-c)] p=(a+b+c)/2 #include <cmath> cmath 是 c++ 语言 ...
- java.lang.InstantiationError: sun.net.ftp.FtpClient
发送邮件功能.本地可以,测试环境上报错.是JDK 版本导致的,,本地1.6 测试环境JDK 1.7 解决办法: 1.测试环境重新配置jdk 1.6 环境.. 2.安装 JDK 1.7 ...
- Oracle 11g 新特性(一)-- 虚拟列
数据库版本: Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Oracle11g 增加了虚拟列的新特性, 具体说明如 ...
- Lucene5.x 中文 同义词
查询好好多资料,英文同义词好好的,中文就不行,多谢网友支持,拼接了好多代码,然后修改了一些,不足之处,多谢指正. 直接上代码吧,在代码中了解怎么分词的最好 1,创建分词引擎 public interf ...
- View.setTag()的作用
//这个东西在一些需要用到Adapter自定控件显示方式的时候非常有用 //Adapter 有个getView方法,可以使用setTag把查找的view缓存起来方便多次重用 public View g ...