[LeetCode] Longest Valid Parentheses
第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n)
// 使用栈,时间复杂度 O(n),空间复杂度 O(n)
class Solution {
public:
int longestValidParentheses(string s) {
int max_len = , last = -;
stack<int> lefts;
for (int i = ; i < s.size(); ++i) {
if (s[i] =='(') {
lefts.push(i);
} else {
if (lefts.empty()) {
// update last
last = i;
} else {
// find a matching pair
lefts.pop();
if (lefts.empty()) {
max_len = max(max_len, i-last);
} else {
max_len = max(max_len, i-lefts.top()); }
}
}
}
return max_len;
}
};
第二种方方法,搞一个计数器,每次遇到'('加一,遇到‘)’ 减少一,所以只有在计数器==0的时候更新 max_len。为了实现功能,必须两侧扫描,从而在计数器==0的时候更新max_len.
例如( ( () () 由于左括号多,因此从左侧扫描无法达到计数器==0,所以,max_len还是0.但当从右侧扫描是,右括号较少,会达到0,从而得到真实的max_len.保存start的思想和栈方法一致。
// LeetCode, Longest Valid Parenthese
// 两遍扫描,时间复杂度 O(n),空间复杂度 O(1)
// @author 曹鹏
class Solution {
public:
int longestValidParentheses(string s)
{
int answer = , depth = , start = -;
for (int i = ; i < s.size(); ++i) {
if (s[i] == '(') {
++depth;
} else {
--depth;
if (depth < ) {
start = i;
depth = ;
} else if (depth == ) {
answer = max(answer, i - start);
}
}
}
depth = ;
start = s.size();
for (int i = s.size() - ; i >= ; --i) {
if (s[i] == ')') {
++depth;
} else {
--depth;
if (depth < ) {
start = i;
depth = ;
} else if (depth == ) {
answer = max(answer, start - i);
}
}
}
return answer;
}
};
[LeetCode] Longest Valid Parentheses的更多相关文章
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Longest Valid Parentheses 解题思路
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [Leetcode] longest valid parentheses 最长的有效括号
Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...
- [LeetCode] 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 -- 挂动态规划羊头卖stack的狗肉
(Version 1.3) 这题在LeetCode上的标签比较有欺骗性,虽然标签写着有DP,但是实际上根本不需要使用动态规划,相反的,使用动态规划反而会在LeetCode OJ上面超时.这题正确的做法 ...
- leetcode: Longest Valid Parentheses分析和实现
题目大意:给出一个只包含字符'('和')'的字符串S,求最长有效括号序列的长度. 很有趣的题目,有助于我们对这种人类自身制定的规则的深入理解,可能我们大多数人都从没有真正理解过怎样一个括号序列是有效的 ...
- leetcode Longest Valid Parentheses python
class Solution(object): def longestValidParentheses(self, s): """ :type s: str :rtype ...
- Java for LeetCode 032 Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- 强连通 HDU 1827
n个点m条边 n个权lcy 要叫这个人的花费 m条边 缩点后 新的图中 入度为0的点要通知 通知强连通分量中权值最小的 #include<stdio.h> #include<alg ...
- UITableView的cell重用优化
三种情况,四种方法: 情况一:加载xib中描述的cell 情况二:加载纯代码自定义的cell 情况三:加载storyBoard中的tableView内的cell 针对于情况一: // 导入自定义cel ...
- Java实现本地 fileCopy
前言: Java中流是重要的内容,基础的文件读写与拷贝知识点是很多面试的考点.故通过本文进行简单测试总结. 2.图展示[文本IO/二进制IO](这是参考自网上的一张总结图,相当经典,方便对比记忆) 3 ...
- VS调试网页的localhost
那个像是iis的服务器叫: 只能访问自己的localhost,也就是127.0.0.1是回环地址(LoopBack),就是表示访问本机,是不用经过网卡的. 别名就是localhost.这种地址无法再外 ...
- 【BZOJ-1090】字符串折叠 区间DP + Hash
1090: [SCOI2003]字符串折叠 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1127 Solved: 737[Submit][Stat ...
- 【poj3461】 Oulipo
http://poj.org/problem?id=3461 (题目链接) 题意 求一个字符串在另一个字符串中出现的次数. Solution KMP裸题,太久没写过了,都忘记怎么求next数组了..水 ...
- 【bzoj1014】 JSOI2008—火星人prefix
http://www.lydsy.com/JudgeOnline/problem.php?id=1014 (题目链接) 题意 给出一个字符串,要求维护这些操作:询问后缀x与后缀y的LCQ(最长公共前缀 ...
- bzoj1396: 识别子串
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- PHP Multipart/form-data remote dos Vulnerability
catalog . Description . Analysis 1. Description PHP is vulnerable to a remote denial of service, cau ...
- mtd零星记录
查看Flash分区情况: root@DD-WRT:~# cat /proc/mtd dev: size erasesize name mtd0: "RedBoot" mtd1: 0 ...