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

标签(空格分隔): LeetCode

作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/decode-ways/description/

题目描述:

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

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

Given a non-empty string containing only digits, determine the total number of ways to decode it.

Example 1:

Input: "12"
Output: 2
Explanation: It could be decoded as "AB" (1 2) or "L" (12).

Example 2:

Input: "226"
Output: 3
Explanation: It could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).

题目大意

输入是一个数字字符串,如果拆分之后其中的部分数字能代表了一个英文字母,那么算作一种拆分方式。求所有共多少拆分方式。

解题方法

这个题目和爬楼梯的题目非常像,直接使用dp。

令, dp[i]代表解析s[:i]字符串所有可能的方式数目。

则:

dp[i] = dp[i-1] if s[i] != '0'
+ dp[i-2] if '9' < s[i-2:i] < '27'

举例子:

对于”226”:

令dp数组为[0,0,0,0],初始化为[1,0,0,0];

从第一个位置开始,输入为”2”,不为”0”,所以dp数组为[1,1,0,0];

第2个位置,输入为”2”,不为”0”,所以dp数组为[1,1,1,0];此时前两位数字是”22”,满足区间,所以变为[1,1,2,0];

第3个位置,输入为”6”,不为”0”,所以dp数组为[1,1,2,2];此时前两位数字是”26”,满足区间,所以变为[1,1,2,3]。

代码如下:

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

参考资料:

  1. https://leetcode.com/problems/decode-ways/discuss/163707/Python-From-O(N)-Space-To-O(1)-Space-Solutions

日期

2018 年 8 月 27 日 ———— 就要开学了!

【LeetCode】91. Decode Ways 解题报告(Python)的更多相关文章

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

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

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

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

  3. 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 ...

  4. LeetCode:Decode Ways 解题报告

    Decode WaysA message containing letters from A-Z is being encoded to numbers using the following map ...

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

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

  6. leetcode 91 Decode Ways ----- java

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

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

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

  8. Leetcode#91 Decode Ways

    原题地址 动态规划题,注意0导致的小陷阱. 代码: int numDecodings(string s) { ] < ] > ; ] >= ] <= : ; ; int nex ...

  9. [LeetCode] 639. Decode Ways II 解码方法 II

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

随机推荐

  1. Linux服务器I/O性能分析-1

    一.IOSTAT误区 1.1 误区-svctm Linux上的svctm是重要的I/O指标(I/O平均服务时间-单位毫秒),这个值直接反映了硬件的性能(I/O请求从SCSI层发出--->I/O完 ...

  2. C#表头固定

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="info.ascx.cs& ...

  3. HDFS06 DataNode

    DataNode 目录 DataNode DataNode工作机制 数据完整性 DataNode掉线时限参数设置 DataNode工作机制 一个数据块在DataNode上以文字形式存储在磁盘上,包括一 ...

  4. Spark中的分区方法详解

    转自:https://blog.csdn.net/dmy1115143060/article/details/82620715 一.Spark数据分区方式简要 在Spark中,RDD(Resilien ...

  5. keil 生成 bin 文件 gd32为例

    fromelf --bin --output .\update\GD32F4xZ.bin .\Output\GD32450Z_EVAL.axf代表使用的keil内的工具代表输出公式,..表示: 输出 ...

  6. Output of C++ Program | Set 16

    Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...

  7. PhoneGap本地将html打包成安卓App

    PhoneGap的在线打包有大小限制,超过30M的包无法在线打包.当然,可以把包里面的图片.声音文件去掉,然后打包.下载以后,解包,重新打包并签名.蛮麻烦的. 本地打包的简单方法如下: 下载安装Jav ...

  8. transient关键字和volatile关键字

    看到HashSet的源代码的时候,有一个关键字不太认识它..transient,百度整理之: Java的Serialization提供了一种持久化对象实例的机制,当持久化对象时,可能有一些特殊的对象数 ...

  9. java面试--(生成随机数,获取重复次数最多,并且数是最大的一个,打印出来)

    import java.util.*; public class MaxRandom { public static void main(String[] args){ int[] num = new ...

  10. java web 限制同一个用户在不同处登入

    用到的技术:map集合,sessionListener监听器,Fiter过滤器. 实现思路: 一.利用一个全局的map集合来保存每个用户sessionID的值的一个集合.一个用户对应一个session ...