leetcode problem 32 -- Longest Valid Parentheses
Longest Valid Parentheses
Given a string containing just the characters '('
and ')'
, find the length of the longest valid (well-formed) parentheses substring.
For "(()"
, the longest valid parentheses substring is "()"
, which has length = 2.
Another example is ")()())"
, where the longest valid parentheses substring is "()()"
, which has length = 4.
思路1:
从左到右依次扫描,用一个数组v记录括号是否已经匹配。然后再扫描一次v,用一个变量count记录连续合法括号数目。如果v[i] = 0则说明该括号未匹配,合法括号序列终端,count = 0. 代码如下:
Runtime: 22 ms
class Solution {
public:
int longestValidParentheses(string s) {
vector<int> v(s.length(), );
stack<int> stac;
for (int i = ; i < s.length(); ++i) {
if (s[i] == ')') {
if (stac.empty())
continue;
else {
v[i] = ;
v[stac.top()] = ;
stac.pop();
}
}
else
stac.push(i);
} int res = , count = ;
for (int i = ; i < s.length(); ++i) {
if (v[i] == ) {
if (res < count)
res = count;
count = ;
}
else if (s[i] == '(')
count += v[i];
}
return res < count ? count : res;
} };
思路二:
先从左到右扫描,分别记录'('和')'的数目, 如果一旦')'的数目大于'(' 那么可以确定合法连续括号序列中断。记录countMax。
然后从右往左扫描,同样分别记录'('和')'的数目, 如果一旦'('的数目大于')' , 那么可以确定合法连续括号序列中断。记录countMax。
代码如下:
Runtime: 10 ms
class Solution {
public:
int longestValidParentheses(string s) {
int ll = , lr = , li = ;
int rl = , rr = , ri = s.length()-;
int res = ;
for (; li < s.length() && ri >= ; ++li, --ri) {
switch (s[li]) {
case '(':
++ll;
break;
case ')':
++lr;
}
switch (s[ri]) {
case '(':
++rl;
break;
case ')':
++rr;
}
if (ll == lr && (ll * ) > res)
res = * ll;
else if (ll < lr)
ll = lr = ; if (rl == rr && (rl * ) > res)
res = * rl;
else if (rl > rr)
rl = rr = ; } return res;
}
private: };
leetcode problem 32 -- Longest Valid Parentheses的更多相关文章
- [Leetcode][Python]32: Longest Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...
- 【一天一道LeetCode】#32. Longest Valid Parentheses
一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(' and ')', find the length of t ...
- 【LeetCode】32. Longest Valid Parentheses (2 solutions)
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- 【LeetCode】32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- 刷题32. Longest Valid Parentheses
一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...
- [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 ...
- Java [leetcode 32]Longest Valid Parentheses
题目描述: Given a string containing just the characters '(' and ')', find the length of the longest vali ...
随机推荐
- [一]初识JFreeChart
是什么? Java报表工具 原理? 封装好数据,调用工厂,创建一张图片,返回一个图片的名字,直接在页面上显示即可 怎么做? 需要导入jar,并在web.xml文件中进行相关的配置即可
- 多目标遗传算法 ------ NSGA-II (部分源码解析) 非支配排序、分层 rank.c
/* Rank assignment routine */ # include <stdio.h> # include <stdlib.h> # include <mat ...
- Python 学习入门(28)—— 服务器实例
在新的Python 3.x中,BaseHTTPServer, SimpleHTTPServer, CGIHTTPServer整合到http.server包,SocketServer改名为sockets ...
- URL与资源
资源推荐 1.HTTP权威指南. <HTTP权威指南>由古尔利所著,<HTTP权威指南>详细解释了HTTP协议,包括HTTP是如何工作的,如何用HTTP来开发基于Web的应用程 ...
- progressBar 自定义
自定义 ProgressBar 进度条 自定义样式[复制链接] 黑牛 黑牛当前离线 威望 33 在线时间 31 小时 金钱 443 贡献 10 诚信度 0 最后登录 2013-10-17 ...
- C++中的类访问控制
C++中 public,protected, private 访问标号小结 第一:private, public, protected 访问标号的访问范围. private:只能由1.该类中的函数.2 ...
- TFS 2010 让安装更简单,也让VSS成为历史
一转眼VS 2010 RC(Release Candidate)版本号已经公布一月多了,RTM(Release To Manufacturer)版本号也快妥了,已经进入了最后的倒计时,仅仅等4月12号 ...
- [React] Using the classnames library for conditional CSS
Classnames is a simple yet versatile javascript utility that joins CSS class names based on a set of ...
- [Javascript] Adding Shapes to Maps with Leaflet and GeoJSON
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- Linux--------------安装mysql(2)
在 CentOS7 上安装 MySQL5.7 1 通过 SecureCRT 连接到阿里云 CentOS7 服务器: 2 进入到目录 /usr/local/ 中:cd /usr/local/ 3 创建目 ...