【leetcode】Decode Ways
题目如下:

解题思路:这个题目的本质是一个爬楼梯问题,在爬楼梯问题中,每次可以选择走一步或者两步。而本题也是一样的,解码的时候选择解码一个字符或者两个字符,但是加大了一点点难度,要考虑这些情况。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的更多相关文章
- 【leetcode】Decode Ways(medium)
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- 【Leetcode】【Medium】Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- 【LeetCode】动态规划(下篇共39题)
[600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
- 【LeetCode】91. Decode Ways 解题报告(Python)
[LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- 【LeetCode】分治法 divide and conquer (共17题)
链接:https://leetcode.com/tag/divide-and-conquer/ [4]Median of Two Sorted Arrays [23]Merge k Sorted Li ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Longest Word in Dictionary through Deleting 解题报告
[LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
随机推荐
- centos官网镜像下载方法
1.CentoS简介 CentOS(Community Enterprise Operating System,社区企业操作系统)是一个基于Red Hat Linux 提供的可自由使用源代码的企业级L ...
- 自动爬取代理IP例子
import time import json import datetime import threading import requests from lxml import etree from ...
- GitHub入门(一)GIT配置与Hexo博客搭建
首先安装配置Git环境,由于本人使用Windows操作系统所以从msysgit.github.io下载msysGit Windows版本,安装.(Mac一般自带Git) 安装的时候一般使用默认选项,其 ...
- Apache web服务器(LAMP架构)
1.apache介绍 1).世界上使用率最高的网站服务器,最高时可达70%:官方网站:apache.org 2).http 超文本协议 HTML 超文本标记语言 3).URL 统一资源定位符 http ...
- java调用com组件com4j
com4j A Java library that allows Java applications to seemlessly interoperate with Microsoft Compone ...
- 记一次Python pip安装失败的总结
pip 安装失败时,可能换此方法可解决1.升级pip版本,这个一般会主动提示python3 -m pip install --upgrade pip2.修改pip源,默认的pip源速度实在无法忍受,或 ...
- Struts2框架学习笔记1
1,框架概述 1.1,什么是框架(了解) 将一些重复性的代码进行封装,简化程序员的编程操作,可以使得程序员在编码中把更多的精力放到业务需求的分析和理解上面,相当于一个半成品软件. 1.2,三大框架(掌 ...
- Springboot2.x集成单节点Redis
Springboot2.x集成单节点Redis 说明 在Springboot 1.x版本中,默认使用Jedis客户端来操作Redis,而在Springboot 2.x 版本中,默认使用Lettuce客 ...
- [Python3] 001 "Hello World" 与注释
目录 1. 致敬 1.1 致敬 "Hello World" 1.2 致敬 Python 之父 Guido van Rossum 2. 注释 2.1 单行注释 2.2 多行注释 3. ...
- SVN与Git的优点差异比较
今天自己还是很有进步的,但是 下午的进度很慢,学习还是得回去,不能在工位进行 在网上看到一篇有关于SVN与Git的区别 复制下来了,以后可以经常看看 一. 集中式vs分布式 1. Subversion ...