[LeetCode] 032. Longest Valid Parentheses (Hard) (C++)
指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql)
Github: https://github.com/illuz/leetcode
032. Longest Valid Parentheses (Hard)
链接:
题目:https://oj.leetcode.com/problems/longest-valid-parentheses/
代码(github):https://github.com/illuz/leetcode
题意:
问一个字符串里最长的合法括号串的长度。
分析:
- (C++)用栈来做,假设匹配就出栈,然后长度就是
cur - stack_top_pos也就是 - 匹配的前一个位置。 O(n) time, O(n) space。 - (C++)栈消耗空间太多了。事实上能够维护 () 匹配的长度。只是可能出现
()))和的情况。所以要前后各扫一遍。
((()O(n) time, O(1) space。
- 用较复杂的 DP 来做,只是空间可没解法 2 那么优了。刚看到我非常久前的一个解法,用太多空间了Orz。如今来看还是 1、2 的做法好。
代码:
解法 1:(C++)
class Solution {
public:
int longestValidParentheses(string s) {
stack<int> lefts;
int max_len = 0, match_pos = -1; // position of first
// matching '(' - 1
for (int i = 0; i < s.size(); ++i) {
if (s[i] == '(')
lefts.push(i);
else {
if (lefts.empty()) // no matching left
match_pos = i;
else { // match a left
lefts.pop();
if (lefts.empty())
max_len = max(max_len, i - match_pos);
else
max_len = max(max_len, i - lefts.top());
}
}
}
return max_len;
}
};
解法 2:(C++)
class Solution {
public:
int longestValidParentheses(string s) {
int max_len = 0, depth = 0, start = -1;
// solve ((()
for (int i = 0; i < s.size(); ++i) {
if (s[i] == '(')
++depth;
else {
--depth;
if (depth == 0)
max_len = max(max_len, i - start);
else if (depth < 0) {
start = i;
depth = 0;
}
}
}
// solve ()))
depth = 0;
start = s.size();
for (int i = s.size(); i >= 0; --i) {
if (s[i] == ')')
++depth;
else {
--depth;
if (depth == 0)
max_len = max(max_len, start - i);
else if (depth < 0) {
start = i;
depth = 0;
}
}
}
return max_len;
}
};
版权声明:本文博客原创文章,博客,未经同意,不得转载。
[LeetCode] 032. Longest Valid Parentheses (Hard) (C++)的更多相关文章
- Java for LeetCode 032 Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- LeetCode 032 Longest Valid Parentheses
题目描述:Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the l ...
- LeetCode 之 Longest Valid Parentheses(栈)
[问题描写叙述] Given a string containing just the characters '(' and ')', find the length of the longest v ...
- [LeetCode] 32. Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- leetcode 32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【leetcode】Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- 【leetcode】 Longest Valid Parentheses (hard)★
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Java [leetcode 32]Longest Valid Parentheses
题目描述: Given a string containing just the characters '(' and ')', find the length of the longest vali ...
- [leetcode]32. Longest Valid Parentheses最长合法括号子串
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- cocos2d-x V3.0 呼叫加速度计 Acceleration
今天克服了一个问题,我觉得非常酷 哈哈. 今天得到解决cocos2d-x 3.0 呼叫重力加速器问题,上网查了很多资料 发现是不够,不解决这个问题,我不知道如果我使用3.0 这一问题的版本号,但是,这 ...
- 重新想象 Windows 8 Store Apps (13) - 控件之 SemanticZoom
原文:重新想象 Windows 8 Store Apps (13) - 控件之 SemanticZoom [源码下载] 重新想象 Windows 8 Store Apps (13) - 控件之 Sem ...
- Java线程Dump分析工具--jstack(转)
jstack用于打印出给定的java进程ID或core file或远程调试服务的Java堆栈信息,如果是在64位机器上,需要指定选项"-J-d64",Windows的jstack使 ...
- 一道看似简单的sql需求(转)
听说这题难住大批高手,你也来试下吧.ps:博问里的博友提出的. 原始数据 select * from t_jeff t 简单排序后数据 select * from t_jeff t order by ...
- 最简单的视频编码器:基于libx265(编码YUV为H.265)
===================================================== 最简单的视频编码器系列文章列表: 最简单的视频编码器:编译 最简单的视频编码器:基于libx ...
- 我已提取并尝试使用启动脚本(./start navicat)来启动 Navicat Linux 版本号,但没有反应
具体的安装教程,參考这个navicat_for_mysql_10.0.11在linux下的安装,介绍的非常具体 參考这个 :我可否在 64-bit Linux 执行 Navicat? 推荐navica ...
- node.js基础:数据存储
无服务器的数据存储 内存存储 var http = require('http'); var count = 0; //服务器访问次数存储在内存中 http.createServer(function ...
- mvc+webapi+dapper+ef codefirst项目搭建
首先项目是mvc5+webapi2.0+orm数据处理(dapper)+ef动态创建数据库. 1.项目框架层次结构: mvc项目根据不同的业务和功能进行不同的区域划分[今后项目维护起来方便],mode ...
- POJ 2405 Beavergnaw (计算几何-简单的问题)
Beavergnaw Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6203 Accepted: 4089 Descri ...
- 卓尼斯ZT-180点评
卓尼斯ZT-180点评 ——我们出差,使用10”上网本发布,没有图片.并写冲忙.遗憾的不足之处. 一.购买 1.由于旅游.不想拿那台14"笔记本,台平板电脑.当时,选择的对象有维智A8 ...