【栈】Longest Valid Parentheses

题目:leetcode

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.

分析:

1、先从左到右扫描。遇到 ‘(‘ 则入栈,反之出栈。每当栈为空时,更新一次返回值res!因为此时已经达到最大值。

2、再从右到左扫描一次。为什么还需要从右到左扫描一次?因为从左到右扫描时结束时,若栈不为空,则有可能还有一部分有效的左右括号没有记录到res中。

 int longestValidParentheses(string s) {
        if(s.size()<2)
        return 0;

        int res=0,cur=0;
        stack<char> sa;
        for(int i=0;i<s.size();i++)
        {
            if(s[i]=='(')
            {
                sa.push(s[i]);
                cur++;
            }
            else
            {
                if(sa.empty())
                {
                    res=max(res,cur);
                    cur=0;
                }
                else
                {
                    sa.pop();
                     cur++;
                    if(sa.empty())
                     res=max(res,cur);

                }
            }
        }

        if(sa.empty())
            return res;

       while(!sa.empty())
       {
            sa.pop();
       }
       cur=0;

        for(int i=s.size()-1;i>=0;i--)
        {
            if(s[i]==')')
            {
                sa.push(s[i]);
                cur++;
            }
            else
            {
                if(sa.empty())
                {
                    res=max(res,cur);
                    cur=0;
                }
                else
                {
                    sa.pop();
                     cur++;
                    if(sa.empty())
                      res=max(res,cur);
                }
            }
        }

        return res;
    }
时间: 2024-11-08 12:30:24

【栈】Longest Valid Parentheses的相关文章

Leetcode 栈 Longest Valid Parentheses

Longest Valid Parentheses Total Accepted: 14818 Total Submissions: 75749My Submissions Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest va

LeetCode 之 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 exa

LeetCode - 32. Longest Valid Parentheses

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

LeetCode: Longest Valid Parentheses O(n)时间 O(1)空间

题目: 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

leetcode第31题--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

Longest Valid Parentheses Leetcode 32 一种奇特的解法

题目: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 i

[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

[LeetCode]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 i