[leedcode 32] Longest Valid Parentheses

Given a string containing just the characters ‘(‘ and ‘)‘, find the length of the longest valid (well-formed) parentheses substring.

For "(()", the longest valid parentheses substring is "()", which has length = 2.

Another example is ")()())", where the longest valid parentheses substring is "()()", which has length = 4.

public class Solution {
    public int longestValidParentheses(String s) {
        //本题很有难度,要求求出最长匹配的括号长度,遇到括号,首先想到stack数据结构
        //注意栈保存的是下标
        //栈中只存左括号,并维护一个变量max
        //遍历一遍s,若是左括号‘(’则直接push下标
        //若是右括号‘)‘,判断此时栈是否为空
        //1)若为空,则用一个变量last记录此时的下标,last初始值是-1
        //2)若栈不为空,表示匹配,弹出栈顶,此时再判断栈是否为空,
        //  i)如果为空,找到了一组匹配字符串max=Math.max(max,i-last);
        // ii)如果不为空,则栈顶的‘(‘还没找到它的‘)‘,因为栈顶元素有可能找不到它的‘)‘,因此,此时要更新 _max = max(_max, i - stack.peek())
        //对于last的解释,有的博客解释是:当出现右括号,此时栈空时,则当前的‘)‘没有一个‘(‘与它匹配,它可以作用于它左右两个匹配的括号串的分割         //点,用一个变量 last 记录下它的坐标。last的初始值是-1,当遇到新的分割点时,我们更新 last,
        //并可以得到两个 last之间的匹配括号的长度")()()"
        Stack<Integer> stack=new Stack<Integer>();
        int last=-1;
        int max=0;
        for(int i=0;i<s.length();i++){
            if(s.charAt(i)==‘(‘)
                stack.push(i);
            else{
                if(stack.empty()){
                    last=i;

                }else{
                    stack.pop();
                    if(stack.empty()){
                        max=Math.max(max,i-last);

                    }else{
                        max=Math.max(max,i-stack.peek());
                    }

                }
            }

        }
        return max;
    }
}
时间: 2024-10-31 16:25:40

[leedcode 32] Longest Valid Parentheses的相关文章

LeetCode - 32. Longest Valid Parentheses

32. Longest Valid Parentheses Problem's Link ---------------------------------------------------------------------------- Mean: 给定一个由'('和')'组成的字符串,求最长连续匹配子串长度. analyse: 定义一个stack<pair<char,int>>类型的stack. 遇到'('进栈; 遇到')'需要分两种情况讨论: 栈顶元素为'(',正好匹配,

刷题32. Longest Valid Parentheses

一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过5次提交终于正确了. 性能如下: Runtime: 8 ms, faster than 61.76% of C++ online submissions for Longest Valid Parentheses. Memory Usage: 9.8 MB, less than 10.71% of

[Leetcode][Python]32: Longest Valid Parentheses

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 32: Longest Valid Parentheseshttps://oj.leetcode.com/problems/longest-valid-parentheses/ Given a string containing just the characters '(' and ')',find the length of the longest valid (well-fo

32. Longest Valid Parentheses

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is &

Java [leetcode 32]Longest Valid Parentheses

题目描述: Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another exampl

32. Longest Valid Parentheses (Stack; DP)

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is &

LeetCode 32 Longest Valid Parentheses(最长有效括号)(*)

翻译 给定一个仅仅包含"("或")"的字符串,找到其中最长有效括号子集的长度. 对于"(()",它的最长有效括号子集是"()",长度为2. 另一个例子")()())",它的最长有效括号子集是"()()",其长度是4. 原文 Given a string containing just the characters '(' and ')', find the length of the l

32. Longest Valid Parentheses *HARD*

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is &

leetCode 32.Longest Valid Parentheses (有效的最大括号) 解题思路和方法

Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length