[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: ' ...
随机推荐
- Insert Function before and after main function
Source code: 1: #include<stdio.h> 2: void myStartupFun (void) __attribute__ ((constructor)); 3 ...
- 基于SpringMVC框架项目Demo
Git地址:https://github.com/JavaWeb1024/SpringMVC 1. 框架简介: 为打造一套集群高可用的框架,集成的技术目前比较成熟,稳定.相关的知识点在网络上也 ...
- 使用Expression做Linq的參數化排序
Linq非常的好用,減少大量的資料庫操作手序,使用具名的類別,減少了在程式中寫SQL寫錯字的可能性,問題來了,如果我想用QueryString中的參數,作為排序的依據,但是因為是具名的類別,不能指定字 ...
- winform学习之----将多个控件的click方法绑定到同一click方法中
public Form3() { InitializeComponent(); button1.Click +=new ...
- empty与isset的一点使用体会
刚在做表单提交的时候,我想检验一下数据是否存在,并用var_dump函数看一下数据.首先看使用isset()的代码 //登录函数 function login(){ if(!isset($_POST) ...
- sql对应C#的类型
- Transform-style和Perspective属性
transform-style属性 transform-style属性是3D空间一个重要属性,指定嵌套元素如何在3D空间中呈现.他主要有两个属性值:flat和preserve-3d. transfor ...
- “通过jumpserver远程登录linux服务器,rz上传文件速度过慢”问题的解决
问题: windows通过jumpserver远程登录到linux服务器,使用rz上传jar包,速度太慢(10k以内). 解决方案: 思路:通过ssh直接登录远程服务器 1.secureCRT-> ...
- jQuery 复合选择器的几个例子
<!-- 本文例子所引用的jQuery版本为 jQuery-1.8.3.min.js Author:博客园小dee --> 一. 复合选择器对checkbox的相关操作 1 <inp ...
- PHP开发绝对不能违背的安全铁则
PHP开发绝对不能违背的安全铁则 [来源] 达内 [编辑] 达内 [时间]2012-12-27 使用 mysql_real_escape_string() 作为用户输入的包装器,就可以避免用 ...