给一个只包含 '(' 和 ')' 的字符串,找出最长的有效(正确关闭)括号子串的长度。
对于 "(()",最长有效括号子串为 "()" ,它的长度是 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. Parallel Programming-使用CancellationTokenSource调度并行运行的Task

    本文主要介绍使用CancellationTokenSource调度并行运行的Task. 一.使用场景 有多个Task并行运行时,如果其中一个Task所运行的程序出现异常,我们想马上终止所有待执行的Ta ...

  2. powermock, 强力模拟

    1. powermock是基于mockito或者easymock,TestNG之上的mock: 2. 提供了对于静态函数,私有函数的mock 3. 下载地址:https://github.com/po ...

  3. pip3 更改安装源

    经常在使用Python的时候需要安装各种模块,而pip是很强大的模块安装工具,但是由于国外官方pypi经常被墙,导致不可用,所以我们最好是将自己使用的pip源更换一下,这样就能解决被墙导致的装不上库的 ...

  4. GSM/GPRS操作示例

    A6(GPRS)  博文转自安信科技: http://wiki.ai-thinker.com/gprs/examples 1.电话接收 相关指令 ATDxxxxx;//xxx为电话号码 示例 打电话 ...

  5. NetScaler VPX在Azure上的部署(二)

    本文是Citrix的工程师协助完成.主要是Citrix的VPX的配置. 导入License   进入NetScaler 中点击管理许可   导入后将有提示,请确认重启.   配置Azure HA 由于 ...

  6. MongoDB优化之三:如何排查MongoDB CPU利用率高的问题

    遇到这个问题,99.9999% 的可能性是「用户使用上不合理导致」,本文主要介绍从应用的角度如何排查 MongoDB CPU 利用率高的问题. Step1: 分析数据库正在执行的请求 用户可以通过 M ...

  7. uboot和内核分区的修改

    随着内核的更新,内核越来越大,uboot给nand的kernel分区默认是2M的 device nand0 <nandflash0>, # parts = 4  #: name       ...

  8. K.I.S.S 原则

    KEEP IT SIMPLE, STUPID! 编写只做一件事情,并且要做好的程序:编写可以在一起工作的程序,编写处理文本流的程序,因为这是通用的接口.这就是UNIX哲学.所有的哲学真 正的浓缩为一个 ...

  9. JAVA基础知识总结4(面向对象特征之一:封装)

    封 装:是指隐藏对象的属性和实现细节,仅对外提供公共访问方式. 好处:将变化隔离:便于使用:提高重用性:安全性. 封装原则:将不需要对外提供的内容都隐藏起来,把属性都隐藏,提供公共方法对其访问. th ...

  10. C# 开发网页的打印版

    在项目中,有一个需求时是需要打印产品页面.但是打印出来的版本和网页上的版本不太一致,有些图片不需要,网页上以tab选项卡显示的内容,都需要在打印页面中看到..等等 CSS针对这种需求,引入了一个@me ...