1、来源LeetCode91

一条包含字母 A-Z 的消息通过以下方式进行了编码:

'A' -> 1
'B' -> 2
...
'Z' -> 26

给定一个只包含数字的非空字符串,请计算解码方法的总数。

示例 1:

输入: "12"
输出: 2
解释: 它可以解码为 "AB"(1 2)或者 "L"(12)。

示例 2:

输入: "226"
输出: 3
解释: 它可以解码为 "BZ" (2 26), "VF" (22 6), 或者 "BBF" (2 2 6) 。

解题代码:

class Solution:
def numDecodings(self, s):
"""
:type s: str
:rtype: int
"""
l_s=len(s)
if l_s==0 or s[0]=='':
return 0
one=1
two=ans=0
for i in range(1,l_s+1):
temp_one=int(s[i-1])
temp_two=int(s[i-2:i]) if i>=2 else None
ans=one*int(temp_one!=0)+two*int(temp_two!=None and temp_two>=10 and temp_two<=26)
one,two=ans,one
return ans

【算法题12 解码方法decode way】的更多相关文章

  1. [Swift]LeetCode91. 解码方法 | Decode Ways

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

  2. leetcode刷题-91解码方法

    题目 一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1'B' -> 2...'Z' -> 26给定一个只包含数字的非空字符串,请计算解码方法的总数. 示例 1: ...

  3. 小小c#算法题 - 12 - Joseph Circle(约瑟夫环)

    约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数(从1开始报数),数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个人又 ...

  4. FCC JS基础算法题(12):Where do I belong(数组排序并找出元素索引)

    题目描述: 先给数组排序,然后找到指定的值在数组的位置,最后返回位置对应的索引.举例:where([1,2,3,4], 1.5) 应该返回 1.因为1.5插入到数组[1,2,3,4]后变成[1,1.5 ...

  5. 每天一道算法题(12)——和为n的连续正数序列或者随机数

    题目:输入一个正数n,输出所有和为n 连续正数序列.例如输入15,由于1+2+3+4+5=4+5+6=7+8=15,所以输出3 个连续序列1-5.4-6 和7-8. 1.思路 尊崇以下策略: (1)对 ...

  6. [LeetCode] Decode Ways 解码方法

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

  7. [LeetCode] Decode Ways II 解码方法之二

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

  8. Newtonsoft.Json C# Json序列化和反序列化工具的使用、类型方法大全 C# 算法题系列(二) 各位相加、整数反转、回文数、罗马数字转整数 C# 算法题系列(一) 两数之和、无重复字符的最长子串 DateTime Tips c#发送邮件,可发送多个附件 MVC图片上传详解

    Newtonsoft.Json C# Json序列化和反序列化工具的使用.类型方法大全   Newtonsoft.Json Newtonsoft.Json 是.Net平台操作Json的工具,他的介绍就 ...

  9. LeetCode OJ:Decode Ways(解码方法)

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

随机推荐

  1. InnoDB: auto-extending data file ./ibdata1 is of a different size 640 pages (rounded down to MB) than specified in the .cnf file: initial 768 pages, max 0 (relevant if non-zero) pages!

    问题描述: centos 安装MySQL $yum install mysql-server 安装之后执行命令mysql 报错: 查看mysql的启动日志: [ERROR] InnoDB: auto- ...

  2. list<> 中find的使用

    昨天要在 std::list<std::string> 中判断是否存在某一字符串 std::string . 我首先想到的是 list迭代+std::string重载的"==&q ...

  3. C++ 函数模板二(函数模板重载)

    //函数模板重载 #include<iostream> using namespace std; /* 函数模板会进行严格的类型匹配,模板类型不提供隐式类型转化 普通函数能够进行自动类型转 ...

  4. cobbler 修改 distro_signatures.json

    edit file in /var/lib/cobbler/distro_signatures.json and restart cobblerd service 转自: https://lists. ...

  5. tcpdf

    将文档整为pdf格式文档 网址:http://www.tcpdf.org/examples.php

  6. greenplum全量恢复gprecoverseg -F出现Unable to connect to database时的相关分析及解决方法

    之前有两位朋友碰到过在对greenplum的系统构架更改后,出现全量恢复gprecoverseg -F也无法正常执行的情况. 报错信息为Unable to connect to database. R ...

  7. (转)Unity笔记之编辑器(Foldout、HelpBox、InspectorTitlebar、Slider、MinMaxSlid ...

    1. Foldout.HelpBox 折叠菜单,大家都知道,不具体解释了,直接代码.因为折叠菜单中必然是有内容才能看到效果,所以顺带把HelpBox(提示框)也说了. [code]csharpcode ...

  8. 边缘检测算子和小波变换提取图像边缘【matlab】

    Roberts边缘检测算子:根据一对互相垂直方向上的差分可用来计算梯度的原理,采用对角线方向相邻两像素之差. 小波变换的方法比较适用于展现夹带在正常信号中的瞬间反常现象,具有方向敏感性.所以可以边缘检 ...

  9. CSS-微信开放UI样式

    下面的链接是微信开放的CSS的样式: http://weui.github.io/weui/ 附上GitHub地址:https://github.com/weui/weui

  10. HTTP错误 401.3

    这是在搭建ASP.NET运行环境时出现的错误,在开启了系统的IIS Web服务器,并安装完.NET FrameWork后, 输入网址:http://127.0.0.1  ,有时会出现401.3的错误, ...