Longest Valid Parentheses 解答
Question
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.
Solution 1 -- Stack
Parenthese类的题目,第一反应是用栈。遇到'('入栈,遇到')'出栈。这题关键在于怎样知道当前的valid parenthese length。
我们用一个变量start来表示当前的第一个'(' 。
( ( ( ) ) ) ) ) (
start start
当栈为空时,进来的第一个左括号的位置为当前的start值。
出栈操作后:
1. 如果栈为空,说明从start开始的括号都匹配了。因此长度为 current - start + 1
2. 如果栈不为空,我们知道弹出栈的一定是都已经匹配了的。因此长度为 current - stack.peek()
因为只扫描了一遍原数组,所以时间复杂度是O(n),空间复杂度是O(n)
public class Solution {
public int longestValidParentheses(String s) {
if (s == null || s.length() < 1) {
return 0;
}
int length = s.length();
Deque<Integer> stack = new LinkedList<Integer>();
int start = 0;
int result = 0;
for (int i = 0; i < length; i++) {
char current = s.charAt(i);
if (current == '(') {
stack.push(i);
} else {
if (stack.isEmpty()) {
start = i + 1;
} else {
stack.pop();
result = stack.isEmpty() ? Math.max(result, i - start + 1) : Math.max(result, i - stack.peek());
}
}
}
return result;
}
}
Solution 2 -- DP
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 ...
- 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 ...
- Longest Valid Parentheses 每每一看到自己的这段没通过的辛酸代码
Longest Valid Parentheses My Submissions Question Solution Total Accepted: 47520 Total Submissions: ...
- [LeetCode] Longest Valid Parentheses 动态规划
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Java for LeetCode 032 Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- editplus批量删除html代码空行
在editplus替换菜单功能里,“查找”功能里输入: ^[ \t]*\n 替换为空,然后“全部替换”即可. 替换时,要选择“正则表达式”选项, 详细:http://www.dedecms8.com/ ...
- NetAnalyzer笔记 之 九 使用C#对HTTP数据还原
[创建时间:2016-05-12 00:19:00] NetAnalyzer下载地址 在NetAnalyzer2016中加入了一个HTTP分析功能,很过用户对此都很感兴趣,那么今天写一下具体的实现方式 ...
- 绘制更Smooth的UI
以前很长一段时间,在自定义控制绘制时,只是简单的定义一个QPainter对象而开始绘画.经常会画一些圆角矩形,甚至是一些不规则的图形.对于不规则的图形来说,如果PS技术不好,或者mask制作的不好,常 ...
- edittext实现粘贴表情
package com.sixin.view; import com.sixin.utile.FaceDataUtil; import android.annotation.SuppressLint; ...
- Eclipse下绿色安装插件Aptana、Swing
本文主要针对Ecplise下绿色安装插件,写本篇博客也是因为笔者在Ecplise下安装Aptana时不断安装出现错误,所以写下自己安装成功以及之前出错的原因,也搜集了许多资料在此一并总结一下吧! Ec ...
- FineUI表单验证
自动编码文本 默认情况下,Label的EncodeText属性为true,会对文本中的HTML进行编码.当然我们也可以设置EncodeText=false,从而将HTML片段赋值给Text属性,请看这 ...
- 浅谈js闭包
相信很多人只知道闭包这个词但是具体是怎么回事就不太清楚了,最近在群里有很多小伙伴讨论这个问题但还是蒙眬眬的赶脚.索性就写了这篇文章来帮助大家一起理解闭包. 变量作用域 闭包其实想明白了很简单,但是在理 ...
- js中的因数分解
方法一: <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8& ...
- C++中的虚函数
代码: #include <iostream> #include <cstring> using namespace std; class Base{ public: virt ...
- Reverse digits of an integer.
class Solution { public: int reverse(int x) { ;//long 是怕中间过程溢出 <<,max=-min-){ ans=ans*+x%; x=x ...