[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 message "12"
, it could be decoded as "AB"
(1 2) or "L"
(12).
The number of ways decoding "12"
is 2.
问题:对给定纯数字字符串解码,求解法的可能情况有多少种。
注:0 开头的字符串算作无效, 例如, "02" 无法解码。
也是一道蛮典型的 DP 问题。
总的解码个数,等于对第一个数字单独解码的情况,加上对前两个数字一起解码的情况。
利用 vector<int> v 记录中间计算结果。 v[i] 表示 s[i...n] 的解码个数。
vector<int> v; int decode(string s, int p){ if (v[p] != -) {
return v[p];
} if (s[] == '') {
v[p] = ;
return ;
} if (s.size() == ) {
v[p] = ;
return ;
} if (s.size() == ) {
int tmp = ;
if (s[] != '') {
tmp++;
} if (stoi(s) <= ) {
tmp++;
}
v[p] = tmp;
return tmp;
} int n1 = ;
if (s[] != '') {
n1 = decode(s.substr(), p+);
} int n2 = ;
if (stoi(s.substr(, )) <= ) {
n2 = decode(s.substr(), p+);
} int res = n1 + n2; v[p] = res;
return res;
} int numDecodings(string s) {
vector<int> tmp(s.size() , -); v = tmp; if(s.size() == ){
return ;
} int res = decode(s, ); return res;
}
这道题解了两次,第一次没有解出来,放在一边,做了其他 DP 题目后,第二次再做,觉得顺畅了许多。
第一次解的时候,思路是分治(Divide and conquer)。
分治主要有三个步骤:
分(Divide) :将原问题分割为类型相同的子问题。
治(Conquer) :分别解决各个子问题
整合(Combine) : 将各个子问题结果整合,得到原问题解。
算法: 将密码字符串从中间分割,分别求解两个子问题,并求解中间不分割的情况,整合三个结果,得到原问题的解。
但是,求解中间不分割情况处理起来比较复杂,就没有继续下去。
第二次解的时候,已经做了一些 DP 问题,思路就往 DP 方向想。
动态规划(Dynamic Programming) 的关键条件有两个:
重叠子问题(overlapping sub-problems) : 子问题和原问题是同类型,解法可重用。这点和分治的前两个步骤类似。
最优子结构(optimal substructure) :子问题的结果,可以比较直接地得到原问题的结果。
对于字符串问题,
若采用分治思路,左右子问题都必须求解,并且还要考虑中间不分割的情况,算法容易变得复杂。
若采用 DP 思路,从左往右一直扫过去,免去了分治中求整合的那一步骤,时间复杂度和分治应该差不多,不过算法实现更加简洁。
[LeetCode] Decode Ways 解题思路的更多相关文章
- LeetCode:Decode Ways 解题报告
Decode WaysA message containing letters from A-Z is being encoded to numbers using the following map ...
- 【LeetCode】91. Decode Ways 解题报告(Python)
[LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- [leetcode]Decode Ways @ Python
原题地址:https://oj.leetcode.com/problems/decode-ways/ 题意: A message containing letters from A-Z is bein ...
- [LeetCode] Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- [LeetCode] Decode Ways [33]
题目 A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A ...
- [LeetCode] Decode Ways II 解码方法之二
A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...
- [LeetCode] Word Break 解题思路
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- [LeetCode] Maximum Gap 解题思路
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- [LeetCode] decode ways 解码方式
A message containing letters fromA-Zis being encoded to numbers using the following mapping: 'A' -&g ...
随机推荐
- IQueryable接口与IEnumberable区别
IEnumerable<T> 泛型类在调用自己的SKip 和 Take 等扩展方法之前数据就已经加载在本地内存里了,而IQueryable<T> 是将Skip ,take 这些 ...
- sar监控系统状态
sar 命令很强大,它可以监控系统所有资源状态,比如平均负载.网卡流量.磁盘状态.内存使用等等. 它不同于其他系统状态监控工具的地方在于,它可以打印历史信息,可以显示当天从零点开始到当前时刻的系统状态 ...
- Qt标准对话框之QColorDialog
Qt中提供了一些标准的对话框,用于实现一些常用的预定义功能,比如本节中将要介绍的颜色对话框——QColorDialog. 在不同的系统平台下,颜色对话框的显示效果可能会有所不同,主要因系统主题风格而异 ...
- 此文件时入口文件index.php
此文件时入口文件index.php <?php //定义一下ThinkPHP框架存放的路径 define('THINK_PATH','./ThinkPHP/'); //定义当前的项目的名称,此处 ...
- PHP 面向对象之自定义类
所谓面向对象就是什么时候什么东西做什么,我们设计类的时候需要想的就是怎么做的内容,那么怎么样的一个类才算是符合OOP的思想呢,答案是:这个类写好之后,在使用的过程中,能准确的代表一个事物,在书写的时候 ...
- ios8.1.2耗电情况严重的解决方法
打开cydia,搜索ifile(威锋源,版本2.1.0-1).打开ifile,进入路径/Applications.里面有许多程序文件,选择适当的进行禁用(ifile可以禁用程序的活动而不完全删除它,这 ...
- arm-linux-gcc中对“inline”的处理
C++对于关键字“inline”的处理大家都知道,C++编译器对于内敛函数就是把它当做一个宏展开.这样可能会增加程序的代码量,却可以减少程序入栈和出栈的此处,从而影响程序的执行速度.但是,C语言中扩展 ...
- 定位- CLGeoencoder - 反编码
#import "ViewController.h" #import "MBProgressHUD+MJ.h" #import <CoreLocation ...
- 30年的Hello world
30 年的 Hello world 转载自:http://www.admin10000.com/document/2398.html 最近我在7月4日这一天所在的那周休假了.休假期间,我利用大把的时间 ...
- hdu 1281
二分图,简单的模板题,不过题目比较难懂: 其中important chess就是删掉它不能够完美匹配,所以就枚举每一个可能删的棋子: 代码: #include <cstdio> #incl ...