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.

参考:http://www.cnblogs.com/easonliu/p/3637429.html

 

C++ 代码实现:

#include<iostream>
#include<string>
#include<stack>
using namespace std;

class Solution
{
public:
    int longestValidParentheses(string s)
    {
        stack<int> st;
        if(s.empty())
            return 0;
        bool a[s.length()];
        size_t i;
        for(i=0; i<s.length(); i++)
        {
            if(s[i]=='(')
            {
                //还没有匹配的都设置为false
                a[i]=false;
                st.push(i);
            }
            else if(!st.empty()&&s[i]==')')
            {
                //每次匹配设置两个为true
                a[i]=true;
                a[st.top()]=true;
                st.pop();
            }
            else
                a[i]=false;
        }
        int max=0;
        int count=0;
        for(i=0; i<s.length(); i++)
        {
            if(a[i])
                count++;
            else
                count=0;
            if(count>max)
                max=count;
        }
        return max;
    }
};

int main()
{
    Solution s;
    string str="(()";
    cout<<s.longestValidParentheses(str)<<endl;
}

 

时间: 2024-11-26 20:17:11

Longest Valid Parentheses的相关文章

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

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

[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 example i

[LeetCode] 20.Valid Parentheses

[题目] Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]

leetcode 20 Valid Parentheses 括号匹配

Given a string containing just the characters '(', ')', '{', '}', '[' and']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]"

LeetCode 20 Valid Parentheses(有效的括号)

翻译 给定一个只包含'(', ')', '{', '}', '[' 和']'的字符串,判断这个输入的字符串是否是有效的. 括号必须在正确的形式下闭合,"()" 和"()[]{}" 是有效的,但是 "(]" 和"([)]" 则不是. 原文 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the

[LeetCode] Valid Parentheses

Given a string containing just the characters '(',')','{','}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" a

Valid Parentheses

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]"

&lt;font color=&quot;red&quot;&gt;[置顶]&lt;/font&gt;

Profile Introduction to Blog 您能看到这篇博客导读是我的荣幸,本博客会持续更新,感谢您的支持,欢迎您的关注与留言.博客有多个专栏,分别是关于 Windows App开发 . UWP(通用Windows平台)开发 . SICP习题解 和 Scheme语言学习 . 算法解析 与 LeetCode等题解 . Android应用开发 ,而最近会添加的文章将主要是算法和Android,不过其它内容也会继续完善. About the Author 独立 Windows App 和

leetcode难度及面试频率

转载自:LeetCode Question Difficulty Distribution               1 Two Sum 2 5 array sort         set Two Pointers 2 Add Two Numbers 3 4 linked list Two Pointers           Math 3 Longest Substring Without Repeating Characters 3 2 string Two Pointers