题目:

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的更多相关文章

  1. leetcode@ [91] Decode Ways (Dynamic Programming)

    https://leetcode.com/problems/decode-ways/ A message containing letters from A-Z is being encoded to ...

  2. Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)

    Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ...

  3. 【LeetCode】91. Decode Ways 解题报告(Python)

    [LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  4. [LeetCode] 91. Decode Ways 解码方法

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  5. 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 ...

  6. 91. Decode Ways反编译字符串

    [抄题]: A message containing letters from A-Z is being encoded to numbers using the following mapping: ...

  7. leetcode 91 Decode Ways ----- java

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  8. 【LeetCode】91. Decode Ways

    题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...

  9. 【一天一道LeetCode】#91. Decode Ways

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 A messa ...

随机推荐

  1. 洛古 P1373 小a和uim之大逃离

    P1373 小a和uim之大逃离 题目提供者lzn 标签 动态规划 洛谷原创 难度 提高+/省选- 题目背景 小a和uim来到雨林中探险.突然一阵北风吹来,一片乌云从北部天边急涌过来,还伴着一道道闪电 ...

  2. ThinkPHP3.2 加载过程(二)

    回顾: 上次介绍了 ThinkPHP 的 Index.PHP入口文件.但只是TP的入口前面的入口(刷boss总是要过好几关才能让你看到 ,不然boss都没面子啊),从Index.PHP最后一行把我们引 ...

  3. 创建型模式——Builder

    1.意图 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. 2.结构 3.参与者 Builder为创建一个Product对象的各个部件指定抽象接口 ConcreteBuild ...

  4. 《RedHatLinux逻辑卷的管理》——一条龙服务

    首先建2分区 [root@localhost ~]# partx -d /dev/sdb error deleting partition 4: BLKPG: No such device or ad ...

  5. 【转】Qt使用自带的windeployqt 生成exe来发布软件

    集成开发环境 QtCreator 目前生成图形界面程序 exe 大致可以分为两类:Qt Widgets Application  和 Qt Quick Application.下面分别介绍这两类exe ...

  6. nginx 重启命令

    #重启nginx sudo /etc/init.d/nginx restart sudo /etc/init.d/nginx Usage: /etc/init.d/nginx {start|stop| ...

  7. 宝马-中国官方网站服务站点信息爬去记录(解析json中数据)

    具体步骤: 1.进入宝马官网,查找经销商查询界面 http://www.bmw.com.cn/cn/zh/general/dealer_locator/content/dealer_locator.h ...

  8. easy ui 下拉框绑定数据select控件

    easy ui 中的下拉框控件叫做select,具体代码如下: html代码:①.这是一个公司等级的下拉框 <tr> <td>公司等级:</td> <td&g ...

  9. 【学习总结】【多线程】 多线程概要 & GDC & NSOperation

    基本需要知道的 :  进程 :  简单点来说就是,操作系统中正在运行的一个应用程序,每个进程之间是独立的,每个进程均运行在受保护的内存空间内 线程 :  一个进程(进程)想执行任务,必须有线程(所以, ...

  10. 关于搭建Android环境的时候遇到 'could not find adb.exe!'的问题

    关于'could not find adb.exe'的问题 问题原因: 文件所处位置和Android_home变量指路径不一致 文件路径: 解决方法: 直接将相关文件退拽至变量值的路径下即可 小结:a ...