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 ...
随机推荐
- TOMCAT清理
CreateTime--2017年7月10日08:54:00Author:Marydon 如何清理TOMCAT 方式一:通过tomcat的安装目录进行清理 找到TOMCAT的根目录,如图: 实质: ...
- ValueError: Expecting property name: line 1 column 1 (char 1)
# -*- coding: cp936 -*- #xiaodeng #python 2.7.10 import weibo s='{"name":"xiaodeng&qu ...
- jQuery正则:电话、身份证、邮箱简单校验
if (!(/^1[3,5,6,7,8,9]\d{9}$/).test(e.detail.value.data_phone)) { wx.showToast({ title: '请输入有效11位手机号 ...
- 微信小程序之分享,动态添加分享数据
1.效果: 2..js代码: page({ /** * 用户点击分享按钮或右上角分享 */ onShareAppMessage: function (res) { var that = this; r ...
- 简单的java实验,涉及到 类继承以及接口问题,方法体的重写(区别于重载)
package test ; abstract class Animal { abstract void cry(); abstract String getAnimalName(); } class ...
- HDUOJ1086You can Solve a Geometry Problem too
You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3 ...
- 【LeetCode】47. Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- Everything:速度最快的文件名搜索工具
http://xbeta.info/everything-search-tool.htm Everything(官网|中文主页|教程)是速度最快的文件名搜索软件.其速度之快令人震惊,百G硬盘几十万个文 ...
- 验证码在后台的编写,并实现点击验证码图片时时发生更新 C# 项目发布到IIS后不能用log4net写日志
验证码在后台的编写,并实现点击验证码图片时时发生更新 验证码在软件中的地位越来越重要,有效防止这种问题对某一个特定注册用户用特定程序暴力破解方式进行不断的登陆尝试:下面就是实现验证码的基本步骤: ...
- 查看mysql日志
1.首先确认你日志是否启用了 MySQL>show variables like 'log_bin'; 2.如果启用了,即ON那日志文件就在MySQL的安装目录的data目录下 3.怎样知道当前 ...