[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 ...
随机推荐
- MDCC 2014移动开发者大会 小礼品展商活动
MDCC 2014移动开发者大会 小礼品展商活动 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdGVzdGNzX2Ru/font/5a6L5L2T/fon ...
- Windows IOT
Windows IOT 开发入门(准备工作) 终于抽出空来了,将最近研究的东西记录下来,物联网,万物皆可联网.然后可以做到智能家居,智能生活,智能城市....一大堆.吹牛的就不说了. 在实际应用中 ...
- WPF 3D:使用GeometryModel3D的BackMaterial
原文 WPF 3D:使用GeometryModel3D的BackMaterial 使用BackMaterial,我们可以定义3D物体的内部材质(或者说是背面),比如,我们定义一个四方体容器,外面现实的 ...
- Android TextView里直接显示图片的三种方法
方法一:重写TextView的onDraw方法,也挺直观就是不太好控制显示完图片后再显示字体所占空间的位置关系.一般假设字体是在图片上重叠的推荐这样写.时间关系,这个不付源代码了. 方法二:利用Tex ...
- MVC与三层架构
我们平时总是将三层架构与MVC混为一谈,殊不知它俩并不是一个概念.下面我来为大家揭晓我所知道的一些真相. 首先,它俩根本不是一个概念. 三层架构是一个分层式的软件体系架构设计,它可适用于任何一个项目. ...
- 【Web探索之旅】第三部分第三课:协议
内容简介 1.第三部分第三课:协议 2.第四部分预告:Web程序员 第三部分第三课:协议 之前的课,我们学习了Client-Server模型的客户端语言和服务器语言. 客户端语言有HTML,CSS和J ...
- Python 显示LinkedIn用户作业
CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-18 @author: guaguastd @name: j ...
- uva133 The Dole Queue ( 约瑟夫环的模拟)
题目链接: 啊哈哈,选我选我 思路是: 相当于模拟约瑟夫环,仅仅只是是从顺逆时针同一时候进行的,然后就是顺逆时针走能够编写一个函数,仅仅只是是走的方向的标志变量相反..还有就是为了(pos+flag+ ...
- HDU 4686 Arc of Dream(递归矩阵加速)
标题效果:你就是给你一程了两个递推公式公式,第一个让你找到n结果项目. 注意需要占用该公式的复发和再构造矩阵. Arc of Dream Time Limit: 2000/2000 MS (Java/ ...
- HPUX在oracle10g安装和卸载缩写
创作品,出自 "深蓝的blog" 博客,欢迎转载,转载时请务必注明出处.否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlong/ ...