91. Decode Ways(动态规划 26个字母解码个数)
2024-10-21 03:48:41
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"
最新文章
- 100 个 Linux 常用命令大全
- Ubuntu13.04安装历险记--Mono,Nginx,Asp.Net一个都不能少
- 使用163CentOS镜像
- js自定义弹窗
- Yar - Yet Another RPC framework for PHP
- 转:2014 年 15 款新评定的最佳 PHP 框架
- Myeclipse普通工程转为Maven工程
- Spring——Web应用中的IoC容器创建(WebApplicationContext根应用上下文的创建过程)
- win10 uwp 绑定静态属性
- Linux入门命令解释(1)
- js介绍
- C博客作业01--分支,顺序结构
- JQuery 数组获取和删除元素
- 括号生成(Java实现)
- Linux查看日志常用命令
- 软工网络15团队作业4——Alpha阶段敏捷冲刺3.0
- python3 CERTIFICATE_VERIFY_FAILED错误 certificate verify failed
- SpringBoot添加对Log4j2的支持
- MySQL(视图、触发器、函数)
- 运维基础工具tmux介绍及使用