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. 500 OOPS: chroot

    FTP登录时报错: 1.500 OOPS: chroot 解决方法:关闭SElinux 2.500 OOPS: vsftpd: refusing to run with writable root i ...

  2. mysql利用yum安装指定数据存放路径

    测试环境: Centos6.5.MySQL5.6.28 yum安装具有速度快,便捷关键是不用编译,编译时间太久了! 01.下载mysql https://mirrors.tuna.tsinghua.e ...

  3. 微信小程序+PHP:动态显示项目倒计时(格式:4天7小时58分钟39秒)

    1.一般我们说的显示秒杀都是指的单条数据,循环我没做. 效果: 2.wxml代码: <p class="endtime_act">距报名截止还有: <block ...

  4. 提高PHP编码的一些技巧

    1.不要使用相对路径 例如 require_once('../../lib/some_class.php'); 该方法有很多缺点: 1)它首先查找指定的php包含路径, 然后查找当前目录 2)如果该脚 ...

  5. 架构-LAMP特级学习(网站大访问量解决方案)

    网站运营要面对的四个问题总结: 1.大访问量(主用负载均衡技术) 2.大存储量 3.访问速度 4.服务器监控 一.大访问量解决方案 超级计算机 = 负载均衡 + 集群 0.反向代理(Nginx等实现) ...

  6. js hasChildNodes()指针对元素节点子节点多个的话 true

    <select multiple size="2"> <option value="bj">北京</option> < ...

  7. 使用DotNetBarcode制作基本常用条码

    核心代码: /// <summary> /// 打印一维码 /// </summary> /// <param name="codeText"> ...

  8. 开源的数据可视化JavaScript图表库:ECharts

    ECharts (Enterprise Charts 商业产品图表库)是基于HTML5 Canvas的一个纯Javascript图表库,提供直观,生动,可交互,可个性化定制的数据可视化图表.创新的拖拽 ...

  9. java日志-纯Java配置使用slf4j配置log4j(转)

    工程目录如下 代码里面用的是slf4j,但是想要用log4j来管理日志,就得添加slf4j本来的jar,然后添加log4j和slf4j箱关联的jar即可. 如果是maven项目的话添加下面的依赖即可 ...

  10. linux extundelete 删除文件恢复

    extundelete是基于Linux的一个数据恢复工具,它通过分析文件系统的日志,解析出所有文件的inode信息,从而可以恢复Linux下主流的ext3,ext4文件系统下被误删除的文件. [问题案 ...