[LeetCode]题解(python):091 Decode Ways
题目来源
https://leetcode.com/problems/decode-ways/
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.
题意分析
Input: 一个字符串
Output:可以编码的方式数目
Conditions:a到z分别对应1到26
题目思路
采用动态规划的思想,dp初始化为[1,1],以两个字符来考虑
1)如果10 <= int(s[i-2:i]) <= 26 and s[i - 1] != "0",说明有两种选择方式,dp[i] = dp[i-1] + dp[i-2]
2)如果刚好等于10或者20,那么dp[i] = dp[i-2]
3)如果s[i-1]不为0,说明不是30,40等数字,dp[i] = dp[i-1]
4)如果不满足以上条件,那么不可能编码成功,返回0
AC代码(Python)
class Solution(object):
def numDecodings(self, s):
"""
:type s: str
:rtype: int
"""
if s == "" or s[0] == "":
return 0
dp = [1,1]
for i in range(2, len(s) + 1):
if 10 <= int(s[i-2:i]) <= 26 and s[i - 1] != "":
dp.append(dp[i-1] + dp[i-2])
elif int(s[i-2:i]) == 10 or int(s[i-2:i]) == 20:
dp.append(dp[i-2])
elif s[i-1] != "":
dp.append(dp[i-1])
else:
return 0
return dp[len(s)]
[LeetCode]题解(python):091 Decode Ways的更多相关文章
- 【LeetCode】091. Decode Ways
题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...
- 动态规划小结 - 一维动态规划 - 时间复杂度 O(n),题 [LeetCode] Jump Game,Decode Ways
引言 一维动态规划根据转移方程,复杂度一般有两种情况. func(i) 只和 func(i-1)有关,时间复杂度是O(n),这种情况下空间复杂度往往可以优化为O(1) func(i) 和 func(1 ...
- Java for LeetCode 091 Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- LeetCode之“动态规划”:Decode Ways
题目链接 题目要求: A message containing letters from A-Z is being encoded to numbers using the following map ...
- [leetcode.com]算法题目 - Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- LeetCode(91) Decode Ways
题目 A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A ...
- 091 Decode Ways 解码方法
包含 A-Z 的字母的消息通过以下规则编码:'A' -> 1'B' -> 2...'Z' -> 26给定一个包含数字的编码消息,请确定解码方法的总数.例如,给定消息为 "1 ...
- 【LeetCode】91. Decode Ways 解题报告(Python)
[LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- [LeetCode] 639. Decode Ways II 解码方法 II
A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...
随机推荐
- Catching Fish[HDU1077]
Catching Fish Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- jQuery回调、递延对象总结(一)jQuery.Callbacks详解
前言: 作为参数传递给另一个函数执行的函数我们称为回调函数,那么该回调又是否是异步的呢,何谓异步,如:作为事件处理器,或作为参数传递给 (setTimeout,setInterval)这样的异步函数, ...
- 配置当前用户使用豆瓣pip源
配置当前用户使用豆瓣pip源 mkdir ~/.pip/ cat ~/.pip/pip.conf [global] index-url = http://pypi.douban.com/simpl ...
- shell字符串和数组
字符串 : 1.单引号: str = 'value' 单引号字符串的限制: 单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的: 单引号字串中不能出现单引号(对单引号使用转义符后也不行); ...
- Solve error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2'
This error happens in Release mode of VS2010, solve this problem by do following: . Go to Project Pa ...
- C++中函数中没写返回值会怎么样?
先看这一段代码: /* P125 清单7.15 使用迭代求第N个Fibonacci数 */ #include <iostream> int fib(int position); int m ...
- lucene 建立索引的过程
时间 -- :: CSDN博客 原文 http://blog.csdn.net/caohaicheng/article/details/ 看lucene主页(http://lucene.apach ...
- metasploit--exploit模块信息
Name Disclosure Date Rank Description ---- ...
- php根据IP地址跳转对应的城市,淘宝REST api调用地址直接使用
<?php // 定义一个函数getIP() function getIP(){ global $ip; if (getenv("HTTP_CLIENT_IP")) { $i ...
- PHP 多个文件上传
关键函数: is_uploaded_file():用于判断指定的文件是否是通过 HTTP POST 上传的,如果是则返回 TRUE.用于防止潜在的攻击者对原本不能通过脚本交互的文件进行非法管理,这可以 ...