【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. ...
随机推荐
- RESR API (一)之Requests
Requests 如果您正在做基于REST的Web服务,您应该忽略request.POST. - Malcom Tredinnick,Django开发团队 REST框架的Request类扩展了标准的H ...
- 2018.03.27 pandas duplicated 和 replace 使用
#.duplicated / .replace import numpy as np import pandas as pd s = pd.Series([1,1,1,1,1,2,3,3,3,4,4, ...
- Flink架构和调度
1.Flink架构 Flink系统的架构与Spark类似,是一个基于Master-Slave风格的架构,如下图所示: Flink集群启动时,会启动一个JobManager进程.至少一个TaskMana ...
- JavaScript对象之--- RegExp
1.概述 正则表达式是描述字符模式的对象. 正则表达式用于对字符串模式匹配以及检索替换: 2.语法 前者为模式,后者为修饰符. var reg = new RegExp( "xyz" ...
- mysql先分组,然后取每个分组中的第2大的记录
文章参考http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/ 首先建表: ...
- 56 道高频 JavaScript 与 ES6+ 的面试题及答案
56 道高频 JavaScript 与 ES6+ 的面试题及答案 :https://segmentfault.com/a/1190000020082089?utm_source=weekly& ...
- CSUST 8.5 早训
## Problem A A - Meeting of Old Friends CodeForces - 714A 题意: 解题说明:此题其实是求两段区间的交集,注意要去除掉交集中的某个点. 题解: ...
- Client does not support authentication protocol requested by server; consider upgrading MySQL client
出现错误 Client does not support authentication protocol requested by server; consider upgrading MySQL c ...
- MFC- 网络编程
一.MFC网络编程 a)CAsyncSocket用于异步非阻塞类,用UDP通信: b)CAsyncSocket的子类(派生类):Csocket同步阻塞类,用于TCP通信: c)通信前,必须调用AfxS ...
- Bootstrap前端框架快速入门专题
1.Bootstrap简介 Bootstrap,出自自 Twitter,是目前最受欢迎的前端框架. Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的前端框架,它简洁灵活,使得 W ...