LeetCode:Longest Valid Parentheses

原创 2016年05月30日 21:08:31

Longest Valid Parentheses




Total Accepted: 63715 Total Submissions: 282480 Difficulty: 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 ")()())", where the longest valid parentheses substring is "()()", which has length = 4.

Subscribe to see which companies asked this question

Hide Similar Problems
 (E) Valid Parentheses


















思路:

使用stack来保存未匹配的“括号”的下标,然后求下标之间的最大差值。


c++ code:

class Solution {
public:
    int longestValidParentheses(string s) {
        int n = s.length();
        stack<int> st;
        for (int i = 0; i < n; i++) {
            if (s[i] == '(') st.push(i);
            else {
                if (!st.empty()) {
                    if (s[st.top()] == '(') st.pop();
                    else st.push(i);
                }
                else st.push(i);
            }
        }
        
        int a = n, b = 0;
        int ans = 0;
        while(!st.empty()) {
            b = st.top();
            st.pop();
            ans = max(ans, a-b-1);
            a = b;
        }
        ans = max(ans, a);
        return ans;
    }
};


版权声明:本文为博主原创文章,转载请注明出处。

相关文章推荐

longest valid parentheses--LeetCode

Given a string containing just the characters '(' and ')', find the length of the longest valid (wel...

Leetcode: Longest Valid Parentheses

Given a string containing just the characters '(' and ')', find the length of the longest valid (we...
  • doc_sgl
  • doc_sgl
  • 2013年10月03日 12:05
  • 3050

leetcode 032 Longest Valid Parentheses

Given a string containing just the characters'(' and ')', find the length of the longest valid (well...

LeetCode_32---Longest Valid Parentheses

Longest Valid Parentheses  Total Accepted: 36685 Total Submissions: 176120My Submissions Quest...

leetcode Longest Valid Parentheses

这个星期没有做题,跑出去玩了一圈。投个实习感觉被鄙视了,回来继续学习算了。 Given a string containing just the characters '(' and...

[LeetCode]32.Longest Valid Parentheses

题目Given a string containing just the characters ‘(’ and ‘)’, find the length of the longest valid (w...

[LeetCode] 最长合法括号 longest valid parentheses

给你一个字符串,该字符串仅仅包含 '(‘ 和 ')',找出最长的合法括号子串。例如:"(()" 的最长合法括号子串是 “()” ,长度是2。")()())" 的最长合法括号子串是 “()()” ,长度...

leetcode 32. Longest Valid Parentheses

leetcode 32. Longest Valid Parentheses 算法就是从左向右扫描,遇到左括号就入栈,遇到右括号有三种情况: 1.栈顶是左括号,那么用2取代那个左括号,这时如果栈顶下边...

LeetCode (32) Longest Valid Parentheses

题目Given a string containing just the characters ‘(’ and ‘)’, find the length of the longest valid (w...
  • fly_yr
  • fly_yr
  • 2015年09月28日 14:19
  • 260

Leetcode #32. Longest Valid Parentheses 最长括号对 结题报告

1 结题思想题目会给出一定的“((())))”等的括号对,问你说最长的有效括号对是多长?这道题,需要用Stack,并且不能单纯的统计(或),且出栈,其大致有如下的规则:1、遇到(要压栈当前位置 2、...
  • MebiuW
  • MebiuW
  • 2016年04月03日 23:06
  • 455
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode:Longest Valid Parentheses
举报原因:
原因补充:

(最多只允许输入30个字)