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. Python中print格式化输出

    截取字符串输出,下面例子将只输出字符串的前3个字母 >>> str="abcdefg" >>> print "%.3s" % ...

  2. git忽略已添加版本控制的文件

    今天使用git做maven项目的版本控制,刚开始搭建项目后,把所有文件全部提交了. 已经提交的文件,gitignore中后配置也无效了. 所以使用以下命令来操作,操作后要提交哦. 1.执行 git r ...

  3. Umbraco中使用Related Links显示内部链接和外部链接

    在Umbraco的论坛里看到的办法,演示了如何在Umbraco中使用Related Links并显示的过程. 原文地址:http://www.nibble.be/?p=48

  4. ABBYY FineReader利用模式提高OCR质量

    提高OCR质量,除了可以使用ABBYY FineReader 12OCR文字识别软件解决纸质文档的复杂结构未出现在电子文档中,或者未正确检测到区域的问题(详见如何提高ABBYY FineReader ...

  5. Android ROM 打包记录

    android5.1平板或手机应用层一些常用的修改项及目录所在: 1.所以系统应用所在的目录: \alps\packages\apps 2.一些系统属性的定义:并且可以仿写自行定义变量 \alps\b ...

  6. Android的WebView控件载入网页显示速度慢的究极解决方案

    Android的WebView控件载入网页显示速度慢的究极解决方案 [转载来源自http://hi.baidu.com/goldchocobo/] 秒(甚至更多)时间才会显示出来.研究了很久,搜遍了国 ...

  7. 如何打造千万级Feed流系统

    from:https://www.cnblogs.com/taozi32/p/9711413.html 在互联网领域,尤其现在的移动互联网时代,Feed流产品是非常常见的,比如我们每天都会用到的朋友圈 ...

  8. org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'parentId' in 'class java.lang.String'

    Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named ' ...

  9. javascript的replace方法的高级应用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. ConcurrentModificationException 详解

    工作中碰到个ConcurrentModificationException.代码如下: List list = ...;for(Iterator iter = list.iterator(); ite ...