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. 轻松利用WayOs修正版配合推广EasyRadius用户微信公众自助平台

    各大平台争相推出微信公共平台服务,EasyRadius也不会OUT!!! EasyRadius已推出微信公共平台自助服务,用户只需要把公众平台设置为开发者模式,并设置专用的地址,就可以实现旗下宽带用户 ...

  2. Web4个实验题目DOM+JS

    实验目的: 1. 掌握DOM对象的基本语法 2. 掌握getElementById函数 3. 掌握getElementsByTagName函数 来源http://www.cnblogs.com/xia ...

  3. OpenGL ES学习资料总结

    从今年春节后开始学习OpenGL ES,发现网上资料很有限,而且良莠不齐,所以整理了一下我学习时用到的资料和一些心得. 1. OpenGL ES1.x参考资料 把NEHE的教程移植到了Android上 ...

  4. GCT之数学公式(几何部分)

    一.平面图形   二.空间几何体

  5. hbase深入了解

    http://blog.csdn.net/u010967382/article/details/37878701?utm_source=tuicool&utm_medium=referral ...

  6. ffmpeg把ts文件转m3u8并切片

    Linux_x86_64流媒体环境:nginx + EasyDarwin-master 客户端播放器:VLC media player 下载windows下的ffmepg二进制版本,请进网站http: ...

  7. 《Thinking in Java》习题——吸血鬼数字

    最近在看<Java编程思想>,这本书非常棒,不愧是Java程序员的圣经.看到第四章,后面有道题目很有意思,于是就自己做了做.

  8. 如何在Datatable中取得每列的数据列宽度

    你用SqlDataAdapter填充DataTable的时候不要用Fill方法而应该用FillSchema方法: using (SqlConnection conn = new SqlConnecti ...

  9. Unity Shader-描边效果

    原文链接:http://blog.csdn.net/puppet_master

  10. Lua点号和冒号区别

    定义的时候冒号默认接收self参数调用的时候冒号默认传递调用者自己为参数而句号要显示传递或接收self参数 -- 例如:句号定义,需要显示传递或接收 a = { x = } function a.fu ...