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. sp_trace_setevent sqlserver跟踪事件及列

    常用几个事件 10 RPC:Completed12 SQL:BatchCompleted43 SP:Completed sp_trace_setevent sp_trace_setevent [ @t ...

  2. Android 英文文档下载地址

    通过英文Android API学习Android技术是一个不错选择,当然养鸡的专业户要小心了,以下分享一些下载英文文档的链接(请使用迅雷下载): https://dl-ssl.google.com/a ...

  3. ContactsContract.Contacts之sort_key

    // 从Contacts表中找出所有联系人 Cursor cursor = context.getContentResolver().query(         ContactsContract.C ...

  4. 如何使php页面中不再出现NOTICE和DEPRECATED的错误提示

    在php.ini配置文件中修改: error_reporting=E_ALL & ~E_NOTICE & ~E_DEPRECATED 亲测有效,拿去用吧

  5. 设置/修改centos上的swap交换分区的方法

    设置centos上的swap交换分区的方法 作为linux世界里最稳定的服务器版本,rhas5一直有很大的应用面,之前一直关注的是freebsd,因为应用的需要,特别在配合mysql和oracle上r ...

  6. HDUOJ1060Leftmost Digit

    Leftmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  7. 面向对象程序设计(OOP设计模式)-结构型模式之装饰器模式的应用与实现

    课程名称:程序设计方法学 实验4:OOP设计模式-结构型模式的应用与实现 时间:2015年11月18日星期三,第3.4节 地点:理1#208 一.实验目的 加深对结构型设计模式的理解以及在开发中的实际 ...

  8. 【LeetCode】104. Maximum Depth of Binary Tree (2 solutions)

    Maximum Depth of Binary Tree  Given a binary tree, find its maximum depth. The maximum depth is the ...

  9. SQL Server里面导出SQL脚本(表数据的insert语句)

    转载自:http://hi.baidu.com/pigarmy/blog/item/109894c445eab0a28326ac5a.html 最近需要导出一个表的数据并生成insert语句,发现SQ ...

  10. CLH队列锁

    http://blog.csdn.net/aesop_wubo/article/details/7533186 CLH锁即Craig, Landin, and Hagersten (CLH) lock ...