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

分析:

方法一:

看完题目之后首先想到了用递归的方法来解决这个问题,但是对于比较长的字符串出现了TLE(超时),下面是用递归实现的代码,这种方法的思想很简单,但是很耗时。

 1 void getnum(string s, int &num)
 2 {
 3     if(s.size()==0 || s.size()==1)
 4     {
 5         if(s.size()==1 && s[0]==‘0‘)
 6             return;
 7         num++;
 8         return;
 9     }
10
11     int a,b;
12     istringstream in1(s.substr(0,1));
13     istringstream in2(s.substr(0,2));
14
15     in1>>a;
16     in2>>b;
17
18     if(a>=1 && a<=9)
19         getnum(s.substr(1), num);
20
21     if(b>=10 && b<=26)
22         getnum(s.substr(2), num);
23 }
24
25 class Solution {
26 public:
27     int numDecodings(string s) {
28         int num=0;
29         getnum(s, num);
30         return num;
31     }
32 };

方法二:

动态规划的思想来分析题目,假设f(n)表示由给定输入的前n个字符构成的字符串所对应的解码方式个数,用a表示第n个字符所代表的数字,b表示由第n-1个字符和第n个字符所构成的二位数字,

如果a>=1 && a<=9,那么第n个字符可以单独解码,如果b>=10 && b<=26,那么第n-1个字符和第n个字符可以组合解码,以下简称a可以解码,b可以解码。考虑四种情况:

(1)a和b都可以解码,不难得出,此时,f(n)=f(n-1)+f(n-2);

(2)如果a可以解码,b不可以解码时,f(n)=f(n-1);

(3)如果a不可以解码,b可以解码时,f(n)=f(n-2);

(4)a和b都不能解码时,f(n)=0。

这种方法的运行速度很快,运行255个测试例子,耗时72ms,而用方法一耗时好几分钟。

 1 class Solution {
 2 public:
 3     int numDecodings(string s) {
 4         if(s.size()==0)
 5             return 0;
 6
 7         int num1=1, num2=0;
 8         int a,b;
 9         istringstream in1(s.substr(0,1));
10         in1>>a;
11         if(a>=1 && a<=9)
12             num2++;
13         if(s.size()==1)
14             return num2;
15
16         for(unsigned i=2; i<=s.size(); i++)
17         {
18             istringstream in1(s.substr(i-1,1));
19             istringstream in2(s.substr(i-2,2));
20             in1>>a;
21             in2>>b;
22             int temp = num1;
23             num1 = num2;
24             num2 = 0;
25             if(a>=1 && a<=9)
26                 num2 += num1;
27
28             if(b>=10 && b<=26)
29                 num2 += temp;
30         }
31         return num2;
32     }
33 };

[LeetCode OJ] Decode Ways

时间: 2024-10-12 05:00:29

[LeetCode OJ] Decode Ways的相关文章

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 动态规划 Decode Ways

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Decode Ways Total Accepted: 8689 Total Submissions: 55465 A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given a

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]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 639 Decode Ways II

首先回顾一下decode ways I 的做法:链接 分情况讨论 if s[i]=='*' 考虑s[i]单独decode,由于s[i]肯定不会为0,因此我们可以放心的dp+=dp1 再考虑s[i-1]和s[i]decode,如果s[i-1]为*,那么**的组合共有15种(11,12,13.....,21,22..26),注意不是9*9=81种,因为我们是s[i-1]和s[i]一起decode,如果是38这样是大于26的 如果s[i-1]不为*,枚举s[i],组合起来小于26即可 if s[i]!

【Leetcode】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 num

[LeetCode] 91. Decode Ways 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 enco

leetcode No91. Decode Ways

Question: 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, Giv

【leetcode】Decode Ways(medium)

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