给一个只包含 '(' 和 ')' 的字符串,找出最长的有效(正确关闭)括号子串的长度。
对于 "(()",最长有效括号子串为 "()" ,它的长度是 2。
另一个例子 ")()())",最长有效括号子串为 "()()",它的长度是 4。
详见:https://leetcode.com/problems/longest-valid-parentheses/description/

Java实现:

start变量来记录合法括号串的起始位置,遍历字符串,如果遇到左括号,则将当前下标压入栈,如果遇到右括号,如果当前栈为空,则将下一个坐标位置记录到start,如果栈不为空,则将栈顶元素取出,此时若栈为空,则更新结果和i - start + 1中的较大值,否则更新结果和i - 栈顶元素中的较大值.

class Solution {
public int longestValidParentheses(String s) {
if(s==null || s.length()==0){
return 0;
}
Stack<Integer> stack = new Stack<Integer>();
int start = 0;
int res = 0;
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='('){
stack.push(i);
}else{
if(stack.isEmpty()){
start = i+1;
}else{
stack.pop();
res = stack.isEmpty()?Math.max(res,i-start+1):Math.max(res,i-stack.peek());
}
}
}
return res;
}
}

参考:https://www.cnblogs.com/grandyang/p/4424731.html

032 Longest Valid Parentheses 最长有效括号的更多相关文章

  1. [LeetCode] Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  2. [leetcode]32. Longest Valid Parentheses最长合法括号子串

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  3. [LeetCode] 32. Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  4. 32. Longest Valid Parentheses最长有效括号

    参考: 1. https://leetcode.com/problems/longest-valid-parentheses/solution/ 2. https://blog.csdn.net/ac ...

  5. [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)

    指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...

  6. [Leetcode] longest valid parentheses 最长的有效括号

    Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...

  7. Java for LeetCode 032 Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  8. LeetCode 032 Longest Valid Parentheses

    题目描述:Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the l ...

  9. Longest Valid Parentheses(最长有效括号)

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

随机推荐

  1. a标签中href="javacript:;" href="javacript:void(0);" href="#"区别

    在使用<a>标签时,经常会绑定其他事件比如onclick,这时候我们会给<a>标签的href属性赋值为“#”,“javacript:;”,“javacript:void(0); ...

  2. 机器学习:Jupyter Notebook中Matplotlib的使用

    一.matplotlib绘制折线图 matplotlib绘图的实质是折线图,将所有的点用直线连接起来,由于距离比较密,看起来像是个平滑的曲线: import matplotlib as mpl:加载m ...

  3. shell入门-awk-1

    awk对于sed grep的优势在于分段,把分隔符分割成一小段一小段,针对段进行匹配,替换,操作,逻辑判断,比较...等 awk的基本操作 [root@wangshaojun ~]# awk -F ' ...

  4. 大内存电脑在vbox安装linux报错

    问题描述: 1.机器:Linux主机,特别是主机为大内存,比如: 4G内存的使用pae内核的Ubuntu系统的thinkpad电脑. 2.情况:使用VirtualBox安装Linux系统时,比如:通过 ...

  5. Learning Python 010 函数 2

    Python 函数 2 函数的参数 位置参数(普通,正常的参数) 随便编写一个求x^n的值的函数power(x, n): def power(x, n): s = 1 while n > 0: ...

  6. 1、Question: prep_reads.info vs. align_summary.txt

    ###参考:https://www.biostars.org/p/163356/ used TopHat to map my reads against their relative referenc ...

  7. Luogu 4868 Preprefix sum

    类似于树状数组维护区间的方法. 每一次询问要求$\sum_{i = 1}^{n}\sum_{j = 1}^{i}a_j$. 展开一下: $\sum_{i = 1}^{n}\sum_{j = 1}^{i ...

  8. java8 创建树结构的数据

    private List<TreeNode> createTree(Integer pid, Map<Integer, List<SysPermission>> m ...

  9. BK Componet Monitor

    Apache a) 启动服务前将监听地址改成0.0.0.0 b) 确认在文件“/etc/httpd/conf.modules.d/00-base.conf“中有加载mod_status模块 c) 新建 ...

  10. The Little Prince

    Chapter 1 That is why, at the age of six, I gave up what might have been a magnificant career as a p ...