91. 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.
链接: http://leetcode.com/problems/decode-ways/
题解:
一看到这题就想到了爬楼梯climb stairs,典型的一维DP。下面就用一维DP来做。先建立数组dp = new int[s.length() + 1], 初始化一个数字的情况dp[0] = 1, 两个数字组成一个两位数字的情况dp[1] = 1。接下来写出循环体,先算一个数字的情况,当s.charAt(i - 1)不为0的时候,dp[i] = dp[i - 1], 否则dp[i] = 0。 接下来考虑两位数字,当由i-2和i-1这两位组成的数字大于等于10,小于等于26时,dp[i] += dp[i - 2], 否则忽略此种情况。
Time Complexity - O(n), Space Complexity - O(n)。
public class Solution {
public int numDecodings(String s) {
if(s == null || s.length() == 0 || s.charAt(0) == '0')
return 0;
int[] dp = new int[s.length() + 1];
dp[0] = 1;
dp[1] = 1; for(int i = 2; i < dp.length; i++) {
int num = Integer.parseInt(s.substring(i - 2, i));
int twoStepsBehind = (num <= 26 && num >= 10) ? dp[i - 2] : 0;
int oneStepBehind = s.charAt(i - 1) != '0' ? dp[i - 1] : 0;
dp[i] = twoStepsBehind + oneStepBehind;
} return dp[s.length()];
}
}
可以继续优化Space Complexity至O(1).
public class Solution {
public int numDecodings(String s) {
if(s == null || s.length() == 0 || s.charAt(0) == '0')
return 0; int first = 1;
int second = 1; for(int i = 2; i < s.length() + 1; i++) {
int num = Integer.parseInt(s.substring(i - 2, i));
int twoStepsBehind = (num >= 10 && num <= 26) ? first : 0;
int oneStepBehind = s.charAt(i - 1) != '0' ? second : 0;
first = second;
second = twoStepsBehind + oneStepBehind;
} return second;
}
}
题外话: 使用DP思想的一些题目 - decode ways, climb stairs, find LCS(longest common subsequence), find longest ascending subsequence, fine longest descending subsequens, 背包问题,poj滑雪,等等。DP这种重要的编程思想要好好学习领会。
二刷:
还是使用dp,新建一个dp数组比较好理解,但空间的优化却不是很熟练。对于dp,还是需要加强狠练。如何才能写出优雅而且精炼的代码是个问题。 Elegant and concise
Java:
Time Complexity - O(n), Space Complexity - O(n)。
public class Solution {
public int numDecodings(String s) {
if (s == null || s.length() == 0 || s.charAt(0) == '0') {
return 0;
}
int len = s.length();
int[] numWays = new int[len + 1];
numWays[0] = 1; // empty string
numWays[1] = 1; // one char
for (int i = 2; i <= len; i++) {
int num = Integer.parseInt(s.substring(i - 2, i));
numWays[i] = (num <= 26 && num >= 10) ? numWays[i - 2] : 0;
numWays[i] += (s.charAt(i - 1) != '0') ? numWays[i - 1] : 0;
}
return numWays[len];
}
}
因为do[i] 只和dp[i - 1]以及dp[i - 2]有关,我们可以优化空间到O(1)。就是使用两个变量来代表numWays[i - 1]和numWays[i - 2], 以及一个变量res来代表它们的和,接下来三个一起倒腾倒腾就好了
Time Complexity - O(n), Space Complexity - O(1)。
public class Solution {
public int numDecodings(String s) {
if (s == null || s.length() == 0 || s.charAt(0) == '0') {
return 0;
}
int len = s.length();
int lastTwoSteps = 1; // empty string
int lastOneStep = 1; // one char
int res = 0;
for (int i = 2; i <= len; i++) {
int num = Integer.parseInt(s.substring(i - 2, i));
res += (num <= 26 && num >= 10) ? lastTwoSteps : 0;
res += (s.charAt(i - 1) != '0') ? lastOneStep : 0;
lastTwoSteps = lastOneStep;
lastOneStep = res;
res = 0;
}
return lastOneStep;
}
}
题外话:
2/11/2016:
时间相当紧张,要复习LC,刷面经,多线程,设计模式,系统设计。自己提速却不是很成功,转进努力吧。要深入思考。
自己与去年9月开始刷题以来,有什么改变和进步呢??? 好像并没有实质性的突破....要说进步的地方,可能就是养成了学习的习惯吧
Reference:
https://leetcode.com/discuss/49719/dp-with-easy-understand-java-solution
https://leetcode.com/discuss/8527/dp-solution-java-for-reference
91. Decode Ways的更多相关文章
- leetcode@ [91] Decode Ways (Dynamic Programming)
https://leetcode.com/problems/decode-ways/ A message containing letters from A-Z is being encoded to ...
- Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)
Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ...
- 【LeetCode】91. Decode Ways 解题报告(Python)
[LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- [LeetCode] 91. Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- leetcode 91 Decode Ways I
令dp[i]为从0到i的总方法数,那么很容易得出dp[i]=dp[i-1]+dp[i-2], 即当我们以i为结尾的时候,可以将i单独作为一个字母decode (dp[i-1]),同时也可以将i和i-1 ...
- 91. Decode Ways反编译字符串
[抄题]: A message containing letters from A-Z is being encoded to numbers using the following mapping: ...
- leetcode 91 Decode Ways ----- java
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- 【LeetCode】91. 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 ...
随机推荐
- ATL一:CWindowImpl
1,#ifdef _ATL_DISABLE_NO_VTABLE#define ATL_NO_VTABLE#else#define ATL_NO_VTABLE __declspec(novtable)# ...
- C++对象的JSON序列化与反序列化探索
一:背景 作为一名C++开发人员,我一直很期待能够像C#与JAVA那样,可以轻松的进行对象的序列化与反序列化,但到目前为止,尚未找到相对完美的解决方案. 本文旨在抛砖引玉,期待有更好的解决方案:同时向 ...
- Poj 2840 Big Clock
1.Link: http://poj.org/problem?id=2840 2.Content: Big Clock Time Limit: 1000MS Memory Limit: 13107 ...
- JAVA_SE复习(OOP1)
面向对象编程(一) 一.继承 1. 在类图表示中,使用一个分为三块的矩形表示一个类.矩形的第一块表示类名,第二块描述这个类的属性及属性的数据类型,第三块描述这个类的操作,也就是方法以及返回类型. ...
- 一次GC问题定位
同事有段代码执行时间过长,需要进行优化, Hashmultimap<Int,Bean> map = ...; for (400w*96) { // 计算过程 Bean = doComput ...
- 《RedHatLinux系统修复视频(通过本地镜像)》
此视频的测试环境: win7下安装的VMware redhatlinux6.3系统的修复 以删掉kernel系统引导故障重新安装kernel为例 基于本地镜像来对系统进行修复 如疑问可留 ...
- MySQL EER反向建表
Database > Synchronize Model... Choose Stored Connection Select the Schemata Choose which to upda ...
- GET 和 POST 两种方式来完成Http接口
程序使用 HTTP 协议和服务器交互主要是进行数据的提交,通常数据的提交是通过 GET 和 POST 两种方式来完成,下面对这两种方式(.net c#)进行一下说明: 1. GET 方式. GET 方 ...
- [DevExpress]ChartControl之饼状图百分比示例
关键代码: using System; using System.Data; using System.Windows.Forms; using DevExpress.XtraCharts; name ...
- 简单风格 在线音乐播放器(支持wav,MP3等)
找了两天终于找到了,支持wav,MP3,其他格式没有测试. 1.修复了jQuery判断ie的bug, 2.修复播放循环 下载地址: http://pan.baidu.com/s/1o6upwHs