32. 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.
链接: http://leetcode.com/problems/longest-valid-parentheses/
题解:
一开始想要尝试跟Valid Parentheses类似的解法,就是维护一个leftCount,一个rightCount,当leftCount < rightCount的时候清零,当leftCount = rightCount时,尝试更新max,结果出错。后来看到曹神的解法,才发现还应该从字符串尾部向前再来一遍。
下面是解法,two passes, Time Complexity - O(n), Space Complexity - O(1)。
public class Solution {
public int longestValidParentheses(String s) { //if left parentheses count > right parentheses count, case always valid
if(s == null || s.length() == 0)
return 0;
int start = -1, depth = 0, max = 0;
for(int i = 0; i < s.length(); i++) {
if(s.charAt(i) == '(')
depth++;
else {
depth--;
if(depth < 0) {
start = i;
depth = 0;
}
if(depth == 0)
max = Math.max(max, i - start);
}
}
depth = 0;
start = s.length();
for(int i = s.length() - 1; i >= 0; i--) { //if right parentheses count > left parentheses count, case always valid
if(s.charAt(i) == ')')
depth++;
else {
depth--;
if(depth < 0) {
start = i;
depth = 0;
}
if(depth == 0)
max = Math.max(max, start - i);
}
}
return max;
}
}
还有一种解法是one pass, 方法是维护一个stack以及一个start index,遇到左括号入栈,遇到右括号出栈,当stack为空时说明左右括号平衡,计算i 与 start的距离, 否则左括号数目>右括号数目,计算i 与当前栈顶元素距离。 有小trick可以设置start = -1,这样代码里计算距离就可以不用 + 1了。还可以稍微简化一下,不过逻辑下面更清楚。
Time Complexity - O(n), Space Complexity - O(n)。
public class Solution {
public int longestValidParentheses(String s) {
if(s == null || s.length() == 0)
return 0;
Stack<Integer> stack = new Stack<>();
int start = 0, max = 0;
for(int i = 0; i < s.length(); i++) {
if(s.charAt(i) == '(') //record index of each '('
stack.push(i);
else {
if(stack.isEmpty()) //try to find first '('
start = i + 1;
else {
stack.pop();
if(stack.isEmpty()) //left num = right num
max = Math.max(max, i - start + 1);
else //left > right, cal max with current top element in stack
max = Math.max(max, i - stack.peek());
}
}
}
return max;
}
}
二刷:
可以用三种方法来做:
- 一种是曹神的方法,使用类似valid parentheses的方法。
- 维护一个depth,一个count。
- 从左向右遍历时并且当char == '('时,depth++,否则char = ')',depth--,这时候我们count++,因为找到了一对valid parenthese
- 当depth == 0的时候,左右括号平衡,可以尝试更新max, max = Math.max(max, count * 2)
- 接下来判断depth是否小于0,小于0的话depth = 0, count = 0,我们从头开始计算。
- 左右各自遍历一遍。从右向左遍历是为了计算类似于"()(()()"这种情况,这时depth always > 0,没办法得到max = 4的结论。
- 一种是一维DP,分好几种情况,画一个decision tree会比较清楚逻辑。
- 维护一个数组max[], 其中max[i]代表以s.charAt(i)结尾的longest valid parentheses的长度。我们考虑接下来集中情况。
- max[0] = 0,因为此时不能组成"()"。所以我们可以直接从 i = 1开始遍历
- 当前字符是'(', max[i] = 0,因为valid parentheses不能以'('结尾
- s.charAt(i - 1) = '(',正好可以组成一对括号。
- 当 i - 2 >= 0,max[i] = max[i - 2] + 2
- 当 i - 2 < 0, max[i] = 2
- 否则s.charAt(i - 1) = ')',此时我们也是继续进行判断
- 此时我们要求出i关于max[i - 1]对称的字符,就是 i - max[i - 1] - 1
- 假如i - max[i - 1] - 1 >= 0,并且 s.charAt(i - max[i - 1] - 1) == '('
- 此时表示从i - max[i - 1] - 1到i这一段都合理,所以这一部分等于max[i - 1] + 2, 我们要继续判断 i - max[i - 1] - 2
- 当i - max[i - 1] - 2 >= 0, 则 max[i] = max[i - 1] + 2 + max[i - max[i - 1] - 2]
- 否则max[i] = max[i - 1] + 2
- 此时表示从i - max[i - 1] - 1到i这一段都合理,所以这一部分等于max[i - 1] + 2, 我们要继续判断 i - max[i - 1] - 2
- 否则max[i] = 0,我们不改变什么
- 假如i - max[i - 1] - 1 >= 0,并且 s.charAt(i - max[i - 1] - 1) == '('
- 此时我们要求出i关于max[i - 1]对称的字符,就是 i - max[i - 1] - 1
- 在开头维护一个res = 0, 每次计算完max[i]之后尝试更新这个res,最后返回的也是这个res.
- 其实还可以继续简化,留给三刷了
否则,当前字符等于')',这时候继续判断几种情况
- 一种是利用一个stack来计算,这个留给三刷了,也是O(n)和O(n)
Java:
Time Complexity - O(n), Space Complexity - O(1)
public class Solution {
public int longestValidParentheses(String s) {
if (s == null || s.length() == 0) {
return 0;
}
int count = 0, max = 0, depth = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '(') {
depth++;
} else {
depth--;
count++;
if (depth == 0) {
max = Math.max(max, count * 2);
}
if (depth < 0) {
depth = 0;
count = 0;
}
}
}
depth = 0;
count = 0;
for (int i = s.length() - 1; i >= 0; i--) {
char c = s.charAt(i);
if (c == ')') {
depth++;
} else {
depth--;
count++;
if (depth == 0) {
max = Math.max(max, count * 2);
}
if (depth < 0) {
depth = 0;
count = 0;
}
}
}
return max;
}
}
DP - Time Complexity - O(n), Space Complexity - O(n)
public class Solution {
public int longestValidParentheses(String s) {
if (s == null || s.length() == 0) {
return 0;
}
int[] max = new int[s.length()]; // max[i] contains longest valid parentheses end at i
int res = 0;
for (int i = 1; i < max.length; i++) {
char c = s.charAt(i);
if (c == '(') {
max[i] = 0;
} else { // c = ')'
if (s.charAt(i - 1) == '(') {
max[i] = i - 2 >= 0 ? max[i - 2] + 2 : 2;
} else {
if (i - max[i - 1] - 1 >= 0 && s.charAt(i - max[i - 1] - 1) == '(') {
max[i] = max[i - 1] + 2 + (i - max[i - 1] - 2 >= 0 ? max[i - max[i - 1] - 2] : 0);
}
}
}
res = Math.max(max[i], res);
}
return res;
}
}
题外话:
1-20-2016
DP一直学得不好, divide and conquer也学得不好,需要多练习多思考。 像Matrix multiply, counting inversion,closest pair,merge sort之类的,一定要多多练习。还有Weighted quick union with path compression, run-length coding, Huffman Tree等等。
三刷:
还是使用曹神的方法,赞曹神思路清晰。
Java:
public class Solution {
public int longestValidParentheses(String s) {
if (s == null || s.length() == 0) return 0;
int max = 0, count = 0, start = 0, len = s.length();
for (int i = 0; i < len; i++) {
if (s.charAt(i) == '(') count++;
else count--;
if (count == 0) max = Math.max(max, i - start + 1);
if (count < 0) {
count = 0;
start = i + 1;
}
}
start = len - 1;
count = 0;
for (int i = len - 1; i >= 0; i--) {
if (s.charAt(i) == ')') count++;
else count--;
if (count == 0) max = Math.max(max, start - i + 1);
if (count < 0) {
count = 0;
start = i - 1;
}
}
return max;
}
}
Reference:
http://weibo.com/cpcs 曹神微博
http://www.cnblogs.com/springfor/p/3869495.html 小莹子
https://leetcode.com/discuss/9156/my-solution-using-one-stack-in-one-pass
https://leetcode.com/discuss/21549/simple-java-solution-o-n-time-one-stack
https://leetcode.com/discuss/8092/my-dp-o-n-solution-without-using-stack
https://leetcode.com/discuss/7609/my-o-n-solution-using-a-stack
32. Longest Valid Parentheses的更多相关文章
- [Leetcode][Python]32: Longest Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...
- 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 ...
- leetcode problem 32 -- Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- 【Python】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
一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(' and ')', find the length of t ...
随机推荐
- TClientDataSet中关于TField、TFieldDef动态创立字段的应用
//使用 TFieldDef 建表: begin with ClientDataSet1.FieldDefs do begin Add('Name' , ftString, 12, True); { ...
- COALESCE在SQL拼接中的大用途
SQL拼接可以使得代码比较灵活,不会那么死板,对于维护也比较方便. 下面是简单的SQL拼接,同时也包含了隐式游标的概念吧,可以遍历表中的每一个字段 -------------------------- ...
- VirtualBox虚拟机安装MSDOS和MINIX2.0.0双系统
1. 在VirtualBox中新建一个MSDOS虚拟机. 2.下载一个MSDOS软盘镜像. 3.启动虚拟机,提示选择安装盘时,选择步骤2下载过来的MSDOS镜像. 4.正常启动进入DOS命令行,用FD ...
- 指向const的指针和const指针
1.指向const的指针:const int *p 或 int const *p 解释:p是一个指针,指向const int类型的常量:指针指向的内容为常量,因此不能改变*p的值,但指针p可以改变,指 ...
- JDBC 学习笔记(三)—— 数据源(数据库连接池):DBCP数据源、C3P0 数据源以及自定义数据源技术
本文目录: 1.应用程序直接获取连接的缺点(图解) 2.使用数据库连接池优化程序性能(图解) 3.可扩展增强某个类方法的功能的三种方式 4.自定 ...
- asp.net中的mysql传参数MySqlParameter
注意在asp.net中传参 string sql="select name,id from user where id=@id"; //@idm不需要引号 MySqlParamet ...
- Codeforces Round #360 (Div. 2) D. Remainders Game 中国剩余定理
题目链接: 题目 D. Remainders Game time limit per test 1 second memory limit per test 256 megabytes 问题描述 To ...
- 【Ural】【1057】Amount of degrees
数位DP 2009年刘聪<浅谈数位类统计问题> 例题一 从组合数 以及 数位DP的角度都可以做…… 首先转化成求1~n内K进制下只有0.1的数的个数: 考虑K进制下第一个为1的位,剩下的数 ...
- Leetcode#80 Remove Duplicates from Sorted Array II
原题地址 简单模拟题. 从先向后遍历,如果重复出现2次以上,就不移动,否则移动到前面去 代码: int removeDuplicates(int A[], int n) { ) return n; ; ...
- 2013 Asia Chengdu Regional Contest
hdu 4786 Fibonacci Tree http://acm.hdu.edu.cn/showproblem.php?pid=4786 copyright@ts 算法源于ts,用最小生成树可以求 ...