[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 ...
随机推荐
- LeetCode OJ学习
一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...
- PL/SQL学习(一)
原文参考:http://plsql-tutorial.com/ 组成: 声明部分(可选) 执行部分(必选) 异常处理(可选) 声明: DECLARE 执行: ...
- 如何使用KMS激活win10和office
首先你需要下载一个kms软件,地址: https://yunpan.cn/cRxVNy2LRXjBt (提取码:d5d8) 然后搭建kms服务器,很简单.启动软件,选择“附加”Tab, 点连接到服务器 ...
- Class TBoundlabel not found and so on..
Class TBoundlabel not found when you put a labeledit into a panel of CategoryPanel then you'll found ...
- 传感器 -UIAccelerometer
// ios 4 之前 UIAccelerometer // ios 5 <CoreMotion/CoreMotion.h> #import "ViewController.h& ...
- 个人笔记--Servlet之过滤器实现权限拦截
一.编写一个Java类实现javax.servlet.Filter接口 package cn.edu.sxu.filter; import java.io.IOException; import ja ...
- delphi xe5 android 开发数据访问server端(一)
第一篇我们破解并安装了xe5 第二篇我们搭建了开发环境 接下来我们开发一个三层的android程序 建立一个webservices stand-alone vcl application 作为手机访 ...
- 【技术贴】解决Program Files文件夹消失
好久不写程序了,今天良心发现,就寻找一下自己是否安装了JDK,习惯性的去C盘的Program Files的文件夹下面去找,次奥,没有这个文件夹.好吧.是在玩我么. 于是 打开cmd 输入如下命令 AT ...
- Android 如何把一个 RelativeLayout或ImageView背景设为透明
在项目中,需要把RelativeLayout 和 ImageView背景设置为透明,怎么实现呢?这里主要通过代码,请参阅以下关键代码: public ImageView imgDetail; pri ...
- swift 学习资源
http://blog.csdn.net/sqc3375177/article/details/29206779