Longest Valid Parentheses--LeetCode

来源:互联网 发布:微信假红包软件生成器 编辑:程序博客网 时间:2024/06/05 05:37

1.题目

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.

2.题意

求最长的有效括号序列长度

3.分析

使用栈,遇到左括号就进栈,遇到右括号则出栈

主要问题在于右括号的的判断
(1)如果栈为空,说明加上当前右括号没有合法序列
(2)否则弹出栈顶元素

弹出后
如果栈为空,则说明当前括号匹配,长度为i-start+1;
如果栈非空,则长度为i-(m.top()+1)+1
因为m.top()+1之后的括号对肯定是已经匹配成功了

只需要一遍扫描,所以时间复杂度是O(n)
空间复杂度是栈的空间,最坏情况是都是左括号,所以是O(n)

4.代码

class Solution {public:    int longestValidParentheses(string s) {        int res = 0;        int start = 0;        stack<int> m;        for(int i = 0; i < s.size(); ++i)        {            if(s[i] == '(')                m.push(i);            else if(s[i] == ')')            {                if(m.empty())                    start = i + 1;                else                {                    m.pop();                    if(m.empty())                        res = max(res, i - start + 1);                    else                        // res = max(res, i - (m.top() + 1) + 1);                        res = max(res, i - m.top());                }            }        }        return res;    }};
原创粉丝点击