[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 ...
随机推荐
- ubuntu 安装phpmyadmin
参考文章: http://www.111cn.net/database/mysql/43957.htm access错误的解决方法 1 首先下载 去官网下载phpmyadmin 2 然后直接 ...
- variable-precision SWAR算法:计算Hamming Weight
variable-precision SWAR算法:计算Hamming Weight 转自我的Github 最近看书看到了一个计算Hamming Weight的算法,觉得挺巧妙的,纪录一下. Hamm ...
- Visual Studio2012中搭建WCF项目
分布式系统:指在系统与系统之间进行通信,系统不再是孤立的,例如:淘宝查看物流信息,或是hao123的天气预报,这些可能都是用的别的系统的web方法. 1.创建空的解决方案 2.新建项目-WCF服务库项 ...
- C# DES
using System; //这个是使用DES的基础 using System.Security.Cryptography; //这个是处理文字编码的前提 using System.Text; // ...
- PHP学习心得(五)——类型
简介 PHP 支持8种基本的数据类型. 四种标量类型: boolean (布尔型) integer (整型) float (浮点型, 也称作 double) string (字符串) 两种复合类型: ...
- LayoutInflater的获取与使用
在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...
- C# net部署图片分布式存储服务器的小案例
如果web服务用户多了,访问多了,用户上传的图片,文件等内容放在一块,想必服务器是承受不住的,这个时候,我们就需要考虑分布式存储的方法了. 如图所示:一个web服务器拖2个图片服务器 如何做到用户上传 ...
- 对指定文件生成数字摘要的MD5工具类
md5特点:压缩性.不可逆性,经常用于传值过程中的值加密或文件加密static char hexdigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', ...
- Uva 1354 Mobile Computing
题目链接 题意: 在一个宽为r 的房间里, 有s个砝码, 每个天平的一端要么挂砝码, 要么挂另一个天平, 并且每个天平要保持平衡. 求使得所有砝码都放在天平上, 且总宽度不超过房间宽度的最大值. 思路 ...
- Sprint5
进展:今天开始进行了登录界面的编写及实现. 燃尽图: 工作照: