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

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

Given an encoded message containing digits, determine the total number of ways to decode it.

For example,
Given encoded message "12", it could be decoded as "AB" (1 2) or "L" (12).

The number of ways decoding "12" is 2.

 class Solution:
def numDecodings(self, s):
"""
:type s: str
:rtype: int
"""
if s=='':
return 0 dp = [0] *(len(s)+1)
dp[0] = 1
dp[1] = int(s[0]!='') for i in range(2,len(s)+1):
if(s[i-1]!=''):
dp[i]=dp[i-1] if s[i-2:i]>'' and s[i-2:i]<'':
dp[i] +=dp[i-2] return dp[len(s)]
## 1 2 1
## 1 0 1 #dp[i] = dp[i-1] if s[i] != "0"
# +dp[i-2] if "09" < s[i-1:i+1] < "27"

91. Decode Ways(动态规划 26个字母解码个数)的更多相关文章

  1. Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)

    Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ...

  2. 【LeetCode】91. Decode Ways 解题报告(Python)

    [LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  3. leetcode@ [91] Decode Ways (Dynamic Programming)

    https://leetcode.com/problems/decode-ways/ A message containing letters from A-Z is being encoded to ...

  4. [LeetCode] 91. Decode Ways 解码方法

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

  5. 91. Decode Ways

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

  6. leetcode 91 Decode Ways I

    令dp[i]为从0到i的总方法数,那么很容易得出dp[i]=dp[i-1]+dp[i-2], 即当我们以i为结尾的时候,可以将i单独作为一个字母decode (dp[i-1]),同时也可以将i和i-1 ...

  7. 91. Decode Ways反编译字符串

    [抄题]: A message containing letters from A-Z is being encoded to numbers using the following mapping: ...

  8. [leetcode]91. Decode Ways解码方法

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

  9. 动态规划之91 decode ways

    题目链接:https://leetcode-cn.com/problems/decode-ways/description/ 参考:https://www.jianshu.com/p/5a604070 ...

随机推荐

  1. EF的代码优先设计

    CodeFirst 用中文说是代码优先,此技术可以让我们先写代码,然后由Entity Framework根据我们的代码建立数据库 接下来用学生这个例子来演示,有学生表,课程表,和成绩表三张表 首先是M ...

  2. Python 字符串处理(转)

    转自:黄聪:Python 字符串操作(替换.删除.截取.复制.连接.比较.查找.包含.大小写转换.分割等) http://www.cnblogs.com/huangcong/archive/2011/ ...

  3. 实现Runnable接口和继承Thread类区别

    如果一个类继承Thread,则不适合资源共享.但是如果实现了Runable接口的话,则很容易的实现资源共享. 实现Runnable接口比继承Thread类所具有的优势: 1):适合多个相同的程序代码的 ...

  4. 【转】Go Channels

    转自: http://kdf5000.com/2017/07/16/Go-Channels/ Golang使用Groutine和channels实现了CSP(Communicating Sequent ...

  5. c#截取图片

    简单的保存数据流 <input name="upImg" style="width: 350px; height: 25px;" size="3 ...

  6. activemq 实战三 了解连接器的URI-Understanding connector URIs

    Before discussing the details of connectors and their role in the overall ActiveMQ architecture, it’ ...

  7. 开发人员必读openstack网络基础2:交换机、路由器、DHCP

    我们在使用openstack的过程中,会遇到创建虚拟机路由器.交换机等,那么1.他们的作用到底是什么?2.DHCP为什么会产生,它的作用是什么? 个人总结:交换机:一般用在同一网段,工作在数据链路层, ...

  8. 3149: [Ctsc2013]复原

    3149: [Ctsc2013]复原 Time Limit: 10 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 95  Solved: 44[ ...

  9. go练习5--生成md5

    import "crypto/md5" import "encoding/hex" //go 生成 md5 func T4_1() { m := md5.New ...

  10. C#生成唯一码方法

    一.时间戳方法 private string CreateId() { TimeSpan ts = DateTime.UtcNow - , , , , , , ); return Convert.To ...