91. Decode Ways(动态规划 26个字母解码个数)
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个字母解码个数)的更多相关文章
- Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)
Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ...
- 【LeetCode】91. Decode Ways 解题报告(Python)
[LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- leetcode@ [91] Decode Ways (Dynamic Programming)
https://leetcode.com/problems/decode-ways/ A message containing letters from A-Z is being encoded to ...
- [LeetCode] 91. Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- 91. Decode Ways
题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...
- 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 ...
- 91. Decode Ways反编译字符串
[抄题]: A message containing letters from A-Z is being encoded to numbers using the following mapping: ...
- [leetcode]91. Decode Ways解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- 动态规划之91 decode ways
题目链接:https://leetcode-cn.com/problems/decode-ways/description/ 参考:https://www.jianshu.com/p/5a604070 ...
随机推荐
- 设置Android应用程序横竖屏显示
设置Android应用程序横竖屏显示有2中方式: 1.在mainfest中增加android:screenOrientation属性 <?xml version="1.0" ...
- GIS-"地理空间大数据与AI的碰撞"学习笔记
1.关系 人工智能>机器学习>神经网络>深度学习 2.机器学习-两个过程 训练/学习过程:样本数据.学习器.模型参数 测试/预测过程:预测.预测值 3.神经网络 机器学习模拟人脑神经 ...
- laravel 模版赋值
1)一般赋值是直接用view助手函数返回的 return view('Index/index', ['key'=>'value']); 2)一般做系统时,我们都会有一个共同控制器,其他控制器继承 ...
- 对C语言进行调试的最好方法是什么?
要了解调试程序的最好方法,首先要分析一下调试过程的三个要素: 应该用什么工具调试一个程序? 用什么办法才能找出程序中的错误? 怎样才能从一开始就避免错误? 应该用什么工具调试一个程序? 有经验的程序员 ...
- LeetCode——Binary Tree Paths
Description: Given a binary tree, return all root-to-leaf paths. For example, given the following bi ...
- Egret打包App 修改App名称和图标 (Egret4.1.0)
图标替换位置在项目res下的drawable这些目录下,用新图标覆盖即可. 这里我用白色图片替换了白鹭默认的图片ic_launcher.png 修改App名字,在res->value->s ...
- 【BZOJ4429】[Nwerc2015] Elementary Math小学数学 最大流
[BZOJ4429][Nwerc2015] Elementary Math小学数学 Description Ellen给她的学生教小学数学.期末考试已经来临了.考试有n个题目,每一个题目学生们都要对一 ...
- 网络费用流-最小k路径覆盖
多校联赛第一场(hdu4862) Jump Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- Linux系统下 Apache+PHP 环境安装搭建
一.安装Apache2.2.221.到官网下载 http://httpd.apache.org/download.cgi ,选择相应的版本 可以先下载到windows系统中,上传到linux, 也可 ...
- POJ 2240 Arbitrage【Bellman_ford坑】
链接: http://poj.org/problem?id=2240 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...