题目如下:

解题思路:这个题目的本质是一个爬楼梯问题,在爬楼梯问题中,每次可以选择走一步或者两步。而本题也是一样的,解码的时候选择解码一个字符或者两个字符,但是加大了一点点难度,要考虑这些情况。1,Z对应的编码是26,所以超过26的连续两个字符不能合并解码,27只能解析成2和7;2,0字符只能和前面的字符合并解码,不能单独作为一个字符解码。

代码如下:

/**
* @param {string} s
* @return {number}
*/
var numDecodings = function(s) {
if(s.length == 0 || parseInt(s) == 0 || s[0] == '0' || s.indexOf('00') != -1){
return 0
}
var ds = ""
for(var i =0 ;i<s.length-1;i++){
if(s[i] == 0){
continue
}
if(s[i+1] != 0){
ds += s[i]
}
else if(s[i] <='2'){
ds += "A"
}
else{
return 0
}
}
if(s[s.length-1] != '0')
ds += s[s.length-1] var dp = new Array(ds.length)
dp[0] = 1 if(ds[0] <='2' && ds[0] > '0' && ds[1] !='A'){
if(ds[0] == 2 && ds[1] >='7'){
dp[1] = 1
}
else{
dp[1] = 2
}
}
else{
dp[1] = 1
} for(var i = 2;i<ds.length;i++) {
if (ds[i] == 'A') {
dp[i] = dp[i - 1]
continue
}
if (ds[i - 1] <= '2' && ds[i - 1] > 0 ) {
if(ds[i-1] == '2' && ds[i] > '6'){
dp[i] = dp[i - 1]
}
else{
dp[i] = dp[i - 1] + dp[i - 2]
}
}
else {
dp[i] = dp[i - 1]
}
}
//console.log(dp)
return dp[ds.length-1]
};

【leetcode】Decode Ways的更多相关文章

  1. 【leetcode】Decode Ways(medium)

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

  2. 【Leetcode】【Medium】Decode Ways

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

  3. 【LeetCode】动态规划(下篇共39题)

    [600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...

  4. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

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

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

  6. 【LeetCode】分治法 divide and conquer (共17题)

    链接:https://leetcode.com/tag/divide-and-conquer/ [4]Median of Two Sorted Arrays [23]Merge k Sorted Li ...

  7. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  8. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  9. 【LeetCode】Longest Word in Dictionary through Deleting 解题报告

    [LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...

随机推荐

  1. 淘淘相关工具类【json,httpClient,id,FTP,exception,cookie(包括共享cookie的设置等)】

    json package com.taotao.common.utils; import java.util.List; import com.fasterxml.jackson.core.JsonP ...

  2. Matlab——矩阵运算 矩阵基本变换操作

    矩阵运算 + 加 - 减 .* 乘 ./ 左除 .\ 右除 .^ 次方 .' 转置 除了加减符号,其余的运算符必须加“.” >> a = : a = >> a- %减法 ans ...

  3. 关于mysql中修改某个字段类型,以及备份表中数据到新建的表中,从新建的表中移除数据到修改过的表中

    1:修改表中某个字段的类型 alter table usertable MODIFY dddd VARCHAR(50); 其中MODIFY是指修改表中字段的属性 alter表示修改表的意思 2:备份表 ...

  4. vue使用笔记二

    es6\es2015特性http://lib.csdn.net/article/reactnative/58021?knId=1405 使用express-generator初始化你的项目目录http ...

  5. mysql简单命令

    库: 增 create database db1:新建一个默认编码的库 create database db1 charset uet8 ;建一个编码为 utf8 的库 删 drop database ...

  6. 区间动态规划 矩阵连乘 Medium

    The multiplication puzzle is played with a row of cards, each containing a single positive integer. ...

  7. 6-1 如何读写csv数据

    >>> from urllib import urlretrieve >>> urlretrieve('http://table.finance.yahoo.com ...

  8. /cat/cpuinfo信息查看

    # 总核数 = 物理CPU个数 X 每颗物理CPU的核数 # 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数 # 查看物理CPU个数cat /proc/cpuinfo| g ...

  9. django学习笔记(四)

    1.请求周期 url> 路由 > 函数或类 > 返回字符串或者模板语言? Form表单提交: 提交 -> url > 函数或类中的方法 - .... HttpRespon ...

  10. wordpress各个文件作用详解

    1.index.php:wordpress核心索引文件,即博客输出文件. 2.license.txt:WordPress GPL许可证文件. 3.my-hacks.php:定义了博客输出之前处理的追加 ...