Problem :已知字符串s,求出其中最长的括号合法组合长度
 
设置两个指针,一个表示左括号open的个数 ,另一个表示右括号close的个数。
 
 
方法:两次遍历操作,第一次从前往后遍历,第二次从后向前遍历。 因此时间复杂度为O(n)
1.从左向右扫描,当遇到左括号时,open++,当遇到右括号时,close++
 
    如果open==close,那么需要对最长长度max进行更新操作  max = Math.max(max , 2*open)
 
    如果 close > open ,说明右括号个数大于左括号个数,已经不满足合法性,则重新设置open=0, close=0
 
2.从右向左扫描,当遇到右括号时close++,当遇到左括号时,open++
     如果close==open,那么更新max即 max = Math.max(max , 2*close)
     如果open>close ,说明右括号个数小于左括号个数,已经不满足合法性,则重新设置open=0 ,close =0
 
参考代码:
 
package leetcode_50;

/***
*
* @author pengfei_zheng
* 最长合法的括号问题
*/
public class Solution32 {
public static int longestValidParentheses(String s) {
int right = 0 , left = 0, ans = 0;
int len = s.length();
for(int i = 0 ; i < len ; i++){
if(s.charAt(i)=='(')
left++;
else
right++;
if(left == right){
ans = Math.max(ans,2*right);
}
else if(right>left)
left = right = 0;
}
left = right = 0;
for(int i = len-1 ; i >= 0 ; i--){
if(s.charAt(i)==')')
right++;
else
left++;
if(right == left)
ans = Math.max(ans,2*left);
else if(left>right)
left = right = 0;
}
return ans;
}
public static void main(String[]args){
String s="()()";
System.out.println(longestValidParentheses(s));
}
}

LeetCode 32 Longest Valid Parentheses(最长合法的括号组合)的更多相关文章

  1. [LeetCode] 32. 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. 32. Longest Valid Parentheses最长有效括号

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

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

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

  5. leetcode 32. Longest Valid Parentheses

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

  6. Java [leetcode 32]Longest Valid Parentheses

    题目描述: Given a string containing just the characters '(' and ')', find the length of the longest vali ...

  7. 032 Longest Valid Parentheses 最长有效括号

    给一个只包含 '(' 和 ')' 的字符串,找出最长的有效(正确关闭)括号子串的长度.对于 "(()",最长有效括号子串为 "()" ,它的长度是 2.另一个例 ...

  8. [LeetCode] 32. Longest Valid Parentheses (hard)

    原题链接 题意: 寻找配对的(),并且返回最长可成功配对长度. 思路 配对的()必须是连续的,比如()((),最长长度为2:()(),最长长度为4. 解法一 dp: 利用dp记录以s[i]为终点时,最 ...

  9. leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、

    20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...

随机推荐

  1. 干货分享 9款精挑细选的HTML5应用

    对于前端开发者来说,分享一些优秀的HTML5应用可以直接拿来用,更重要的是可以激发创作的灵感.今天我们要分享9款精挑细选的HTML5应用,个个都是干货. 1.HTML5/CSS3滑块动画菜单 图标动画 ...

  2. jmm 和线程安全

    Java的内存模型JMM Java的内存模型JMM(Java Memory Model)JMM主要是为了规定了线程和内存之间的一些关系.根据JMM的设计,系统存在一个主内存(Main Memory), ...

  3. eclipse断点Source not found解决方案1,2,3

    1.tomcat插件 路径是Window --> Preferences --> Tomcat --> Source Path,在Source Path 标签下有行文字:Add ja ...

  4. [译]Angular-ui 之 Url Routing

    ◄ 前一篇 (Multiple Named Views)     下一篇 (The Components) ► 在你的应用中多数的状态都是基于特定的url地址的.Url Routing机制绝不是在状态 ...

  5. kafka学习之-java api测试

    1.配置 package com.storm.storm_my.kafka; /** * * @author Peng.Li * */ public class KafkaConfigApiConst ...

  6. 5 JInja2模版(适用于Django和Flask)

    模版 在生产环节下,我们要把后端程序(其实就是python)计算出来的数据和html页面结合起来做,这个时候模版就派上大用处了. Flask下的模版---Jinja2 Jinja是日本寺庙的意思,并且 ...

  7. mysql中如何开启binlog?开启二进制日志文件?binary log?

    需求描述: 开启mysql的binlog即binary log日志功能,在此记录下. 版本描述: mysql版本:5.7.21-log 操作过程: 1.修改my.cnf并且将以下参数加入其中,重启my ...

  8. 系统安装SQL Sever2000后1433端口未开放,如何打开1433端口的解决方法

    这篇文章主要针对Win2003系统安装SQL Sever2000后1433端口未开放,如何打开1433端口的解决方法. 用了几年的Windows2003和SQL Server2000了,不过这个问题倒 ...

  9. 第六种方式,python使用cached_property缓存装饰器和自定义cached_class_property装饰器,动态添加类属性(三),selnium webdriver类无限实例化控制成单浏览器。

    使用 from lazy_object_proxy.utils import cached_property,使用这个装饰器. 由于官方的行数比较少,所以可以直接复制出来用自己的. class cac ...

  10. [Module] 08 - MVP by Mosby

    From: Mosby MVP使用教程[作者用心] View是消极视图(Passive View), 它尽量不去主动做事, 让Presenter通过抽象方式控制View 例子: 例如Presenter ...