[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)

 1 class Solution(object):
 2     def numDecodings(self, s):
 3         """
 4         :type s: str
 5         :rtype: int
 6         """
 7         if s == "" or s[0] == "0":
 8             return 0
 9         dp = [1,1]
10         for i in range(2, len(s) + 1):
11             if 10 <= int(s[i-2:i]) <= 26 and s[i - 1] != "0":
12                 dp.append(dp[i-1] + dp[i-2])
13             elif int(s[i-2:i]) == 10 or int(s[i-2:i]) == 20:
14                 dp.append(dp[i-2])
15             elif s[i-1] != "0":
16                 dp.append(dp[i-1])
17             else:
18                 return 0
19         return dp[len(s)]
20  
时间: 2024-08-03 20:38:42

[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: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example,Given enco

LeetCode之“动态规划”: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, Gi

091. Decode Ways

1 class Solution { 2 public: 3 int numDecodings(string s) { 4 if (s.size() == 0) return 0; 5 else { 6 vector<int> ways(s.size(), 0); 7 if (s[0] >= '1' && s[0] <= '9') ways[0] = 1; 8 else ways[0] = 0; 9 if (s[1] >= '1' && s[1

[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)

全部最新的题解可以在 我的 github 上找,欢迎 star 和 watch ~ 更新中~~ 说明 这个系列的题解包括用 C++/Java/Python 写的 leetcode 上的算法题目,和 Sql 写的 leetcode 上的数据库题目. 有些题目虽然 AC 了却还没写分析,所以这次就开坑来完成. 链接: 我的 github Leetcode Algorithms Problems Leetcode Database Problems CSDN 题解索引 001.Two_Sum (Med

Decode Ways leetcode java

题目: 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 enc

【leetcode刷题笔记】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

[LeetCode]题解(python):031-Next Permutation

题目来源 https://leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible

[LeetCode]91.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 encode

LeetCode之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