[LeetCode] decode ways 解码方式

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为空,或者其首字母为‘0‘的情况,这时,因为编码是,数字是从1开始的,所以这两种情况下返回值都是0;当为字符串中间时,当前字符有两种编码方式,一是单独,二是和前面的字符组合解码;

1)当前面一个字符为‘1‘时,当前字符可以为0~9的任意一个,而为‘2’时,当前字符要小于‘7‘,此时,当前字符既可以单独解码也可以和前面的联合起来解码dp[i]=dp[i-1]+dp[i-2];

2)若在字符串中间遇到‘0‘,因为‘0‘能组成的只有两种‘10‘和‘20‘,即,当前字符为‘0‘时,只能和前面的字母组合,即dp[i]=dp[i-2];

3)而前面的字符大于‘2‘时,当前字符只能是单独解码,即解码的方式在前一个的基础上不会增加dp[i]=dp[i-1]

所以,先根据当前字符是否为‘0‘先给dp[i]赋值:dp[i]=(s[i-1]==‘0‘?0:dp[i-1]):然后,若是满足条件一,则dp[i]+=dp[i-2];代码如下:

 1 class Solution {
 2 public:
 3     int numDecodings(string s)
 4     {
 5         if(s.empty()||s.size()&&s[0]==‘0‘)  return 0;
 6         vector<int> dp(s.size()+1,0);
 7
 8         dp[0]=1;
 9         for(int i=1;i<=s.size();++i)
10         {
11             dp[i]=(s[i-1]==‘0‘?0:dp[i-1]);
12             if(i>1&&s[i-2]==‘1‘||(s[i-2]==‘2‘&&s[i-1]<=‘6‘))
13                 dp[i]+=dp[i-2];
14         }
15         return dp.back();
16     }
17 };
时间: 2024-10-28 22:46:53

[LeetCode] decode ways 解码方式的相关文章

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 encoded

[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 91. Decode Ways 解码方法(动态规划,字符串处理)

Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给一串包含数字的加密报文,求有多少种解码方式 举个例子,已知报文"12",它可以解码为AB(1 2),也可以是L (12) 所以解码方式有2种. 测试样例 Input: "0" "121212" "1010

[LeetCode] Decode Ways(DP)

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] Decode Ways [33]

题目 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 [090]

[题目] 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 en

LeetCode:Decode Ways 解题报告

Decode WaysA message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total number of ways to decode it. For example,Given e

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

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 enc