题目如下:

解题思路:这个题目的本质是一个爬楼梯问题,在爬楼梯问题中,每次可以选择走一步或者两步。而本题也是一样的,解码的时候选择解码一个字符或者两个字符,但是加大了一点点难度,要考虑这些情况。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. Quaternions 四元数

    四元数是一个乱七八糟得到东西还没搞懂搞懂后再补 先添加unity API transform.rotation 是Quaternionlei类并非Vector3向量 不能进行直接转化 那如何将Vect ...

  2. Application.CreateForm()和TForm.Create()创建的窗体有什么区别么?二者在使用上各有什么技巧?(50分)

    https://wedelphi.com/t/135849/ 请详细些,并给出例子.谢谢. Application.CreateForm()创建的第一个可显示的窗体是自动成为主窗体,并且自动显示,并且 ...

  3. ES6中数组和对象的扩展运算符拷贝问题以及常用的深浅拷贝方法

    在ES6中新增了扩展运算符可以对数组和对象进行操作.有时候会遇到数组和对象的拷贝,可能会用到扩展运算符.那么这个扩展运算符到底是深拷贝还是浅拷贝呢? 一..使用扩展运算符拷贝 首先是下面的代码. le ...

  4. java调用com组件com4j

    com4j A Java library that allows Java applications to seemlessly interoperate with Microsoft Compone ...

  5. vimiumC的下载、配置与节点个性化

    vimium是chrome的一款扩展程序,正如其名:vim+chromium,它能让你在浏览网页时双手不离开键盘,是提上网高效率的神器. 最近在使用中,非常便捷高效,但关于节点的个性化资料比较少,自己 ...

  6. 【MM系列】SAP 的库存管理

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP 的库存管理   前言部分 大 ...

  7. 【ABAP系列】SAP ABAP 运算符

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 运算符   前 ...

  8. 20191127 Spring Boot官方文档学习(4.14-4.17)

    4.14.使用RestTemplate调用REST服务 如果需要从应用程序调用远程REST服务,则可以使用Spring Framework的RestTemplate类.由于RestTemplate实例 ...

  9. 运用swagger编写api文档

    一.什么是swagger 随着互联网技术的发展,前后端技术在各自的道路上越走越远,他们之间的唯一联系变成了api接口,api接口文档编程了前后端人员的纽带,而swagger就是书写api文档的一款框架 ...

  10. Python yield用法浅析(stackoverflow)

    这是stackoverflow上一个关于python中yield用法的帖子,这里翻译自投票最高的一个回答,原文链接 here 问题 Python中yield关键字的用途是什么?它有什么作用?例如,我试 ...