[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' - ...
随机推荐
- Ubuntu 下安装Kibana和logstash
原文地址:http://www.cnblogs.com/saintaxl/p/3946667.html 简单来讲他具体的工作流程就是 logstash agent 监控并过滤日志,将过滤后的日志内容发 ...
- Bzoj 3505: [Cqoi2014]数三角形 数论
3505: [Cqoi2014]数三角形 Time Limits: 1000 ms Memory Limits: 524288 KB Detailed Limits Description
- 初学python(print使用、条件分支、循环、模块引用)
import random """ #查看源代码日后爬虫用 import urllib.request # coding=utf-8 url = "http:/ ...
- [LeetCode] 42. Trapping Rain Water 解题思路
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- uvaoj 10397 - Connect the Campus【最小生成树】
uvaoj 10397 - Connect the Campus Many new buildings are under construction on the campus of the Univ ...
- 从MSN上拔下来的全世界国家下拉框(附带SQL执行脚本)
<select> <option value="AL">阿尔巴尼亚</option> <option value="DZ&quo ...
- mvn创建web项目
1. 新建maven项目,选择maven骨架maven-archetype-webapp来建立web项目 2. 选择next,输入groupid:MavenWebTest, artifactid:cn ...
- ubuntu给手机建wifi
声明 笔者近期意外的发现 笔者的个人站点http://tiankonguse.com/ 的非常多文章被其他站点转载,可是转载时未声明文章来源或參考自 http://tiankonguse.com/ 站 ...
- labview下UDP通信
本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 因项目需要,用labview写了个UDP通信测试程序,原理图如下: 前面板 后面板: 运行效果:
- cocos2d-x 2.1.4学习笔记之HelloWorld分析
下面截图是HelloWorld项目下的文件夹结构 这是用python命令生成的项目,在创建过程中默认生成多个平台的程序文件. 1.“resource”文件夹 该文件夹主要用于存放游戏中需要的图片.音频 ...