[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(超时),下面是用递归实现的代码,这种方法的思想很简单,但是很耗时。
void getnum(string s, int &num)
{
if(s.size()== || s.size()==)
{
if(s.size()== && s[]=='')
return;
num++;
return;
} int a,b;
istringstream in1(s.substr(,));
istringstream in2(s.substr(,)); in1>>a;
in2>>b; if(a>= && a<=)
getnum(s.substr(), num); if(b>= && b<=)
getnum(s.substr(), num);
} class Solution {
public:
int numDecodings(string s) {
int num=;
getnum(s, num);
return num;
}
};
方法二:
用动态规划的思想来分析题目,假设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,而用方法一耗时好几分钟。
class Solution {
public:
int numDecodings(string s) {
if(s.size()==)
return ;
int num1=, num2=;
int a,b;
istringstream in1(s.substr(,));
in1>>a;
if(a>= && a<=)
num2++;
if(s.size()==)
return num2;
for(unsigned i=; i<=s.size(); i++)
{
istringstream in1(s.substr(i-,));
istringstream in2(s.substr(i-,));
in1>>a;
in2>>b;
int temp = num1;
num1 = num2;
num2 = ;
if(a>= && a<=)
num2 += num1;
if(b>= && b<=)
num2 += temp;
}
return num2;
}
};
[LeetCode OJ] Decode Ways的更多相关文章
- leetcode@ [91] Decode Ways (Dynamic Programming)
https://leetcode.com/problems/decode-ways/ A message containing letters from A-Z is being encoded to ...
- 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 ...
- Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)
Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ...
- [LeetCode] 639. Decode Ways II 解码方法 II
A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...
- [LeetCode] 91. Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- leetcode 639 Decode Ways II
首先回顾一下decode ways I 的做法:链接 分情况讨论 if s[i]=='*' 考虑s[i]单独decode,由于s[i]肯定不会为0,因此我们可以放心的dp+=dp1 再考虑s[i-1] ...
- 【leetcode】Decode Ways(medium)
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- leetcode 91 Decode Ways ----- java
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- [leetcode]91. Decode Ways解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
随机推荐
- [hadoop转载]tearsort
1TB排序通常用于衡量分布式数据处理框架的数据处理能力.Terasort是Hadoop中的的一个排序作业,在2008年,Hadoop在1TB排序基准评估中赢得第一名,耗时209秒.那么Tera ...
- 点分治练习:不虚就是要AK
[题面] 不虚就是要AK(czyak.c/.cpp/.pas) 2s 128M czy很火.因为又有人说他虚了.为了证明他不虚,他决定要在这次比赛AK. 现在他正在和别人玩一个游戏:在一棵树上随机取两 ...
- UVA 3890 Most Distant Point from the Sea(二分法+半平面交)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=11358 [思路] 二分法+半平面交 二分与海边的的距离,由法向量可 ...
- 基于iOS,Android的服务器证书失效检测
1.前言 在目前的iOS,Android手机上,当手机应用进行SSL通信时,手机端默认是不会进行服务器证书是否失效的监测. 在iOS上,系统是会定期获取所访问服务器的证书信息然后出存在本地. 在And ...
- storm单机版安装配置
1,install zeromq 期间可能出现:configure: error: cannot link with -luuid, install uuid-dev. 因此可以先安装 sudo ap ...
- 安装MYSQL 出现Error 1045 access denied 的解决方法
操作系统:WINDOWS10 系统 数据库版本:mysql 5.x 提示:access denied for user 'root'@'localhost' using password yes/no ...
- java String的比较,BOX装箱拆箱,以及面向对象的小代码
package cn.hncu.day2; public class StringDemo { public static void main(String[] args) { String str1 ...
- struts2 ajax 实现方式
在 struts2 中实现ajax,可以使用struts2-json-plugin扩展,但是返回的json字段必须都是Action中的属性,不可以随意的输出文本. 返回任意的文本有两种方式, 方法一: ...
- linux find grep使用
在当前目录下所有文件中查找内容包含 string 的文件: find ./ -name "*" -exec grep "string" {} \; 注意:在最后 ...
- Effective C++ 笔记三 资源管理
条款13:以对象管理资源 许多资源被动态分配于heap内而后被用于单一区块或函数内.它们应该在控制流离开那个区块或函数时被释放.标准程序库提供的auto_ptr正是针对这种形式而设计的特制产品.aut ...