LeetCode
  • Introduction
  • 第一章: 基本结构
    • 1.1 数组
      • Q11. Container With Most Water
      • Q16. 3Sum Closest
      • Q118. Pascal's Triangle
      • Q119. Pascal's Triangle II
      • Q120. Triangle
      • Q134. Gas Station
    • 1.2 链表
      • Q2: Add Two Numbers
      • Q19. Remove Nth Node From End of List
      • Q82. Remove Duplicates from Sorted List II
      • Q86: Partition List
      • Q92. Reverse Linked List II
      • Q141. Linked List Cycle
      • Q142. Linked List Cycle II
      • Q147. Insertion Sort List
      • Q160. Intersection of Two Linked Lists
      • Q206. Reverse Linked List
    • 1.3 哈希
      • Q1: Two Sum
      • Q3. Longest Substring Without Repeating Characters
    • 1.4 堆栈
      • Q84: Largest Rectangle in Histogram
      • Q155. Min Stack
      • Q20. Valid Parentheses
      • Q225. Implement Stack using Queues
      • Q232. Implement Queue using Stacks
    • 1.5 树
      • Q94. Binary Tree Inorder Traversal
      • Q100. Same Tree
      • Q101. Symmetric Tree
      • Q102. Binary Tree Level Order Traversal
      • Q103. Binary Tree Zigzag Level Order Traversal
      • Q104. Maximum Depth of Binary Tree
      • Q105. Construct Binary Tree from Preorder and Inorder Traversal
      • Q106. Construct Binary Tree from Inorder and Postorder Traversal
      • Q107. Binary Tree Level Order Traversal II
      • Q108. Convert Sorted Array to Binary Search Tree
      • Q109. Convert Sorted List to Binary Search Tree
      • Q110. Balanced Binary Tree
      • Q111. Minimum Depth of Binary Tree
      • Q112. Path Sum
      • Q113. Path Sum II
      • Q114. Flatten Binary Tree to Linked List
      • Q116. Populating Next Right Pointers in Each Node
      • Q117. Populating Next Right Pointers in Each Node II
      • Q129. Sum Root to Leaf Numbers
      • Q144. Binary Tree Preorder Traversal
    • 1.6 图
    • 1.7 二进制
      • Q89. Gray Code
      • Q136. Single Number
      • Q137. Single Number II
      • Q191. Number of 1 Bits
      • Q190. Reverse Bits
    • 1.8 字符串
      • Q5. Longest Palindromic Substring
      • Q14. Longest Common Prefix
      • Q125. Valid Palindrome
  • 第二章: 动态规划
    • Q85: Maximal Rectangle
    • Q91. Decode Ways
    • Q121. Best Time to Buy and Sell Stock
    • Q198. House Robber
  • 第三章: 递归
    • Q17. Letter Combinations of a Phone Number
    • Q78. Subsets
    • Q86. Scramble String
    • Q90: Subsets II
  • 第四章:贪心
    • Q122. Best Time to Buy and Sell Stock II
  • 第五章:分治法
  • 第六章:数学
    • Q6. ZigZag Conversion
    • Q7. Reverse Integer
    • Q9. Palindrome Number
    • Q168. Excel Sheet Column Title
    • Q171. Excel Sheet Column Number
  • 第七章:查找
    • Q15. Three Sum
    • Q167. Two Sum II
    • Q169. Majority Element
  • 第八章:排序
Powered by GitBook
On this page
  • 分析:
  • C++代码:

Was this helpful?

  1. 第二章: 动态规划

Q91. Decode Ways

PreviousQ85: Maximal RectangleNextQ121. Best Time to Buy and Sell Stock

Last updated 5 years ago

Was this helpful?

直达:

A message containing letters fromA-Zis 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.

分析:

当遍历到字符串 s 的第k个位置时,对应的路径的数量取决于第k-1和k-2个位置的内容和路径数。所以要采用动态规划求解,具体要分成下面几种情况:

  1. s[i] == 0

    1. 如果0<(s[i-1]-'0')*10+s[i]-'0'<=26, 则第k个位置的路径数等于第k-2个位置的路径数, 例如*10*,要从左侧*处断开;

    2. 反之,路径数为0,例如*30*.无法继续,路径数为0,直接返回0。

  2. s[i] != 0

    1. 如果s[i-1]==0

      1. 如果0<(s[i-1]-'0')*10+s[i]-'0'<=26,则第k个位置的路径数等于第k-1个位置的路径数;

      2. 否则,路径数为0,直接返回,如*00*这种情况

    2. 如果s[i-1] != 0

      1. 如果0<(s[i-1]-'0')*10+s[i]-'0'<=26,则第k个位置的路径数等于第k-1个位置的路径数加上k-2个位置的路径数,如*123*,i指向3,断成*12,3*或者*1,23*都可以;

      2. 否则,则第k个位置的路径数等于第k-1个位置的路径数,如*128*只能断成*12,8*。

C++代码:

class Solution {
public:
    int numDecodings(string s) {
        int n = s.size();
        if(n == 0) return 0;
        if(s[0] == '0') return 0;
        vector<int> dp(n+1, 0);
        dp[0] = 1;
        dp[1] = 1;
        for(int i = 1; i < n; i++){
            int temp = ((s[i-1]-'0')*10+s[i]-'0');
            if(s[i] == '0'){
                if (0 < temp && 26 >= temp) dp[i+1] = dp[i-1];
                else return 0;
            }else{
                if(s[i-1] != '0'){
                    if (0 < temp && 26 >= temp) dp[i+1] = dp[i] + dp[i-1];
                    else dp[i+1] = dp[i];
                }else{
                    if (0 < temp && 26 >= temp) dp[i+1] = dp[i];
                    else return 0;
                }
            }
        }
        return dp[n];
    }
};
https://leetcode.com/problems/decode-ways/description/