题目如下:

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

代码如下:

  1. /**
  2. * @param {string} s
  3. * @return {number}
  4. */
  5. var numDecodings = function(s) {
  6. if(s.length == 0 || parseInt(s) == 0 || s[0] == '0' || s.indexOf('00') != -1){
  7. return 0
  8. }
  9. var ds = ""
  10. for(var i =0 ;i<s.length-1;i++){
  11. if(s[i] == 0){
  12. continue
  13. }
  14. if(s[i+1] != 0){
  15. ds += s[i]
  16. }
  17. else if(s[i] <='2'){
  18. ds += "A"
  19. }
  20. else{
  21. return 0
  22. }
  23. }
  24. if(s[s.length-1] != '0')
  25. ds += s[s.length-1]
  26.  
  27. var dp = new Array(ds.length)
  28. dp[0] = 1
  29.  
  30. if(ds[0] <='2' && ds[0] > '0' && ds[1] !='A'){
  31. if(ds[0] == 2 && ds[1] >='7'){
  32. dp[1] = 1
  33. }
  34. else{
  35. dp[1] = 2
  36. }
  37. }
  38. else{
  39. dp[1] = 1
  40. }
  41.  
  42. for(var i = 2;i<ds.length;i++) {
  43. if (ds[i] == 'A') {
  44. dp[i] = dp[i - 1]
  45. continue
  46. }
  47. if (ds[i - 1] <= '2' && ds[i - 1] > 0 ) {
  48. if(ds[i-1] == '2' && ds[i] > '6'){
  49. dp[i] = dp[i - 1]
  50. }
  51. else{
  52. dp[i] = dp[i - 1] + dp[i - 2]
  53. }
  54. }
  55. else {
  56. dp[i] = dp[i - 1]
  57. }
  58. }
  59. //console.log(dp)
  60. return dp[ds.length-1]
  61. };

【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. kafka 通信报文格式

    1. 序列化一条消息 消息有 key 和 value kafka 提供了基础数据类型的序列化工具,对于业务的自定义类需要自行实现序列化 ProducerRecord 是对象,含 KV 和 header ...

  2. 阶段3 1.Mybatis_11.Mybatis的缓存_5 缓存的概念

    2.Mybatis中的缓存     什么是缓存         存在于内存中的临时数据.     为什么使用缓存         减少和数据库的交互次数,提高执行效率.     什么样的数据能使用缓存 ...

  3. Jmeter之非GUI模式(命令行)执行

    在使用Jmeter进行性能测试时,建议使用非GUI模式执行. 命令行启动 1.进入jmeter安装的bin目录 2.执行Jmeter命令 如下: (1.jmeter.bat -n -t E:\apac ...

  4. 纯css闪烁效果

    .i-i-box-active{ -webkit-animation: shan666 1s infinite ease-in-out; animation: shan666 1s infinite ...

  5. Django中Cookie和Session配置和操作

    Cookie Cookie以键值对Key-Value形势进行信息的存储. Cookie基于域名安全,不同域名的Cookie是不能互相访问的 Cookie是存储在浏览器中的一段纯文本信息,建议不要存储敏 ...

  6. springboot文件上传报错

    异常信息: org.springframework.web.multipart.MultipartException: Could not parse multipart servlet reques ...

  7. android 程序的运行步骤(备忘)

    java代码: public class HelloWorld { public static void main(String[] args) { System.out.println(" ...

  8. 应用安全 - CMS - Discuz漏洞汇总

    SSV-90861 Date:2012 类型:敏感信息泄露 影响范围:DZ x2.5  POC:http://www.xx.xx/uc_server/control/admin/db.php http ...

  9. ---Mock---基本使用

    一.mock解决的问题 开发时,后端还没完成数据输出,前端只好写静态模拟数据.数据太长了,将数据写在js文件里,完成后挨个改url.某些逻辑复杂的代码,加入或去除模拟数据时得小心翼翼.想要尽可能还原真 ...

  10. return语句

    定义一个函数:可以定义一个由自己想要功能的函数,以下是简单的规则: (1).函数代码块以 def 关键词开头,后接函数标识符名称和圆括号(). (2).任何传入参数和自变量必须放在圆括号中间.圆括号之 ...