LeetCode:Decode Ways 解题报告
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 解题报告的更多相关文章
- 【LeetCode】91. Decode Ways 解题报告(Python)
		
[LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
 - [LeetCode] Decode Ways 解题思路
		
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
 - LeetCode: Combination Sum 解题报告
		
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
 - [LeetCode] Decode Ways 解码方法
		
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
 - [LeetCode] Decode Ways II 解码方法之二
		
A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...
 - 【LeetCode】Permutations 解题报告
		
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
 - LeetCode - Course Schedule  解题报告
		
以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...
 - LeetCode: Sort Colors 解题报告
		
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
 - 【LeetCode】535. Encode and Decode TinyURL 解题报告(Python & C++)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:数组 方法二:字典 日期 题目地址:https://l ...
 
随机推荐
- eclipse 导入tortoiseSVN检出项目,不显示svn信息(eclipse安装svn插件)
			
eclipse 导入tortoiseSVN检出项目,不显示svn信息(eclipse安装svn插件) CreateTime--2018年5月10日14:10:35 Author:Marydon 1 ...
 - CSS3动画效果之Transform
			
无意中翻看博客发现这个属性,就顺便熟悉了一下,百度了一下和查看了CSS3帮助文档,特整理一下 Transform 适应于对任一DOM元素的2D或3D转换,转换效果有:旋转.拉伸.平移.倾斜等. 目前浏 ...
 - 用户研究Q&A(1)
			
近来,不少同事开始认同用户研究的价值,希望通过接触,理解和研究用户来获取提升产品的有效信息.这绝对是件好事,因为我一直抱持的理念是,研究并不是藏在实验室或者握在少部分人手中的稀罕货,更重要是一种理念和 ...
 - 开源APP 源码
			
作者:wjh2005链接:http://www.zhihu.com/question/28518265/answer/88750562来源:知乎著作权归作者所有,转载请联系作者获得授权. 1. Cod ...
 - LinkedHashMap插入无序且链式操作
			
Iterator<Entry<Integer, Integer>> ite=lhmap.entrySet().iterator(); ite.next(); ite.remov ...
 - Java compiler level does not match解决方法(转)
			
本文转自:https://www.cnblogs.com/lauer0246/p/5740572.html#undefined 从别的地方导入一个项目的时候,经常会遇到eclipse/Myeclips ...
 - CoreData 增删改查
			
#pragma mark - Core Data Methods - (void)insertObjectWithFileName:(NSString *)fileName { /** SQL新增记录 ...
 - jQuery动态网格瀑布流插件Masonry
			
Masonry是一款非常强大的jQuery动态网格布局插件,可以帮助开发人员快速开发瀑布流界面效果.和CSS中float的效果不太一样的地方在于,float先水平排列,然后再垂直排列,使用Masonr ...
 - Hadoop Archives
			
原文地址:http://hadoop.apache.org/docs/r1.0.4/cn/hadoop_archives.html 什么是Hadoop archives? 如何创建archive? 如 ...
 - CListCtrl获取列数
			
CListCtrl获取列数 // m_List是一个CListCtrl CHeaderCtrl* pHeaderCtrl = m_List.GetHeaderCtrl();if(pHeaderCtrl ...