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 1:

我们使用DP来处理这个题目。算是比较简单基础的一维DP啦。

1. D[i] 表示前i个字符能解的方法。

2. D[i] 有2种解法:

1). 最后一个字符单独解码。 如果可以解码,则解法中可以加上D[i - 1]

2). 最后一个字符与上一个字符一起解码。 如果可以解码,则解法中可以加上D[i - 2]

以上2种分别判断一下1个,或是2个是不是合法的解码即可。

 public class Solution {
public int numDecodings(String s) {
if (s == null || s.length() == 0) {
return 0;
} int len = s.length(); // D[i] 表示含有i个字符的子串的DECODE WAYS.
int[] D = new int[len + 1]; D[0] = 1; for (int i = 1; i <= len; i++) {
D[i] = 0; // 现在正在考察的字符的索引.
int index = i - 1;
// 最后一个字符独立解码
if (isValidSingle(s.charAt(index))) {
D[i] += D[i - 1];
} // 最后一个字符与上一个字符一起解码
if (i > 1 && isValidTwo(s.substring(index - 1, index + 1))) {
D[i] += D[i - 2];
}
} return D[len];
} public boolean isValidSingle(char c) {
if (c >= '1' && c <= '9') {
return true;
} return false;
} public boolean isValidTwo(String s) {
int num = Integer.parseInt(s); return (num >= 10 && num <= 26);
}
}

2015.1.3 redo:

public class Solution {
public int numDecodings(String s) {
if (s == null || s.length() == 0) {
return 0;
} int len = s.length(); // The result of first i digits.
int[] D = new int[len + 1]; for (int i = 0; i <= len; i++) {
if (i == 0) {
D[i] = 1;
} else {
// i >= 1
D[i] = 0;
if (i >= 2 && isValid(s.substring(i - 2, i))) {
D[i] += D[i - 2];
} // The digit should not be 0.
if (s.charAt(i - 1) != '0') {
D[i] += D[i - 1];
}
}
} return D[len];
} public boolean isValid(String s) {
int num = Integer.parseInt(s); return num >= 10 && num <= 26;
}
}

SOLUTION 2:

http://www.ninechapter.com/solutions/

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/dp/NumDecodings.java

LeetCode:Decode Ways 解题报告的更多相关文章

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

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

  2. [LeetCode] Decode Ways 解题思路

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

  3. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  4. [LeetCode] Decode Ways 解码方法

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

  5. [LeetCode] Decode Ways II 解码方法之二

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

  6. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  7. LeetCode - Course Schedule 解题报告

    以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...

  8. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  9. 【LeetCode】535. Encode and Decode TinyURL 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:数组 方法二:字典 日期 题目地址:https://l ...

随机推荐

  1. onkeydown-onkeypress-onkeyup

      CreateTime--2016年12月17日22:28:36Author:Marydononkeydown.onkeypress和onkeyup参考链接:http://www.jb51.net/ ...

  2. 【SPSS】软件介绍

    SPSS软件是美国斯坦福大学三位学生1968年研制开发的统计软件,SPSS是Statistical Package for Social Science(社会科学软件统计包)的缩写,2000年SPSS ...

  3. python之函数用法round()

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法round() #http://www.cnblogs.com/hongfei/p/3 ...

  4. Linux密钥认证错误解决

    问题描述: Xshell用key认证登录,提示所选的用户密钥未在远程主机上注册 问题解决: 查看日志/var/log/secure,基本上都是用户根目录的权限问题 根据日志提示: Authentica ...

  5. java操作hdfs到数据库或者缓存

    使用hadoop工具将数据分析出来以后,须要做入库处理或者存到缓存中.不然就没了意义 一下是使用javaAPI操作hdfs存入缓存的代码: <span style="font-fami ...

  6. 让 linux centos 文件夹地址栏 位置栏显示出来的方法

    今天又拿起心爱的 linux ,发现多日不用又忘记了不少知识了    , 发现忘记的速度真是惊人的! 设置方法: 编辑-> 首选项-> 勾选 总是在浏览器窗口中打开,  如图:

  7. Web前端开发笔试&面试_02(others)

    AL>> 1.CSS 3 如何实现旋转图片? 答:transform : rotate 2.写CSS 的工具? 答:LESS.SASS 3.JavaScript 倒计时? 答:setTim ...

  8. PowerDesigner 12小技巧-pd小技巧-pd工具栏不见了-pd修改外键命名规则-pd添加外键

    PowerDesigner 12小技巧-pd小技巧-pd工具栏不见了-pd修改外键命名规则-pd添加外键 1. 附加:工具栏不见了 调色板(Palette)快捷工具栏不见了PowerDesigner ...

  9. activity的onCreate参数 saveInstanceState

        写过Android程序的都知道Activity中有一个名称叫onCreate的方法.该方法是在Activity创建时被系统调用,是一个Activity生命周期的开始.可是有一点容易被忽视,就是 ...

  10. Linux内核(7) - 设备模型(上)

    对于驱动开发来说,设备模型的理解是根本,毫不夸张得说,理解了设备模型,再去看那些五花八门的驱动程序,你会发现自己站在了另一个高度,从而有了一种俯视的感觉,就像凤姐俯视知音和故事会,韩峰同志俯视女下属. ...