关闭

LeetCode:Longest Valid Parentheses

标签: LeetCodeLongest Valid Parent
3023人阅读 评论(1) 收藏 举报
分类:

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;
    }
};


0
0
查看评论

LeetCode(32)Longest Valid Parentheses

题目如下: Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substr...
  • feliciafay
  • feliciafay
  • 2014-03-07 08:31
  • 1749

Longest Valid Parentheses -- LeetCode

原题链接: http://oj.leetcode.com/problems/longest-valid-parentheses/  这道题是求最长的括号序列,比较容易想到用栈这个数据结构。基本思路就是维护一个栈,遇到左括号就进栈,遇到右括号则出栈,并且判断当前合法序列是否为最长序...
  • linhuanmars
  • linhuanmars
  • 2014-03-04 07:57
  • 11167

LeetCode Longest Valid Parentheses 解题报告

分析,求出一串由:‘(’和‘)’组成的字符串中最长有效括号的长度。例如:(()(),结果是4。((()))结果是6。())()()结果是4。
  • worldwindjp
  • worldwindjp
  • 2014-09-22 00:24
  • 8898

Leetcode 32 Longest Valid Parentheses DP好题

就喜欢做这种想法题, 一开始的想法和之前做过的一道括号匹配差不多,用栈。 记录未匹配的括号位置,然后两两相减找到最大值,过了,但速度并不快。 在discuss中看到有人用DP,恍然大悟! dp[i]表示以当前位置为终点的最长长度,则只能在)处更新, 如果s[i-1-dp[i-1]]=='(&...
  • u012614906
  • u012614906
  • 2016-09-05 13:43
  • 1316

[leetcode-32]Longest Valid Parentheses(java)

问题描述: Given a string containing just the characters ‘(’ and ‘)’, find the length of the longest valid (well-formed) parentheses substring.For “(()”, ...
  • zdavb
  • zdavb
  • 2015-09-01 16:54
  • 556

Longest Valid Parentheses,二维动态规划、一维动态规划、stack

一、二维动态规划 int longestValidParentheses(string s) {         int len=s.length(),maxLen=0;         bool valid[len][...
  • angelewings
  • angelewings
  • 2014-12-10 10:31
  • 718

【LeetCode】Longest Valid Parentheses 解题报告

【题目】 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-forme...
  • ljiabin
  • ljiabin
  • 2015-04-06 19:05
  • 1789

LeetCode --- 32. Longest Valid Parentheses

题目链接:Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length of the longest valid (well-...
  • makuiyu
  • makuiyu
  • 2015-02-04 23:16
  • 1542

[Leetcode]32. Longest Valid Parentheses @python

题目Given a string containing just the characters ‘(’ and ‘)’, find the length of the longest valid (well-formed) parentheses substring.For “(()”, the l...
  • qian2729
  • qian2729
  • 2016-01-17 11:30
  • 308

LeetCode 之 Longest Valid Parentheses(栈)

LeetCode 之 Valid Palindrome,本文给出详细算法思想与源码实现。
  • u013630349
  • u013630349
  • 2015-07-23 10:07
  • 736
    个人资料
    • 访问:390374次
    • 积分:6789
    • 等级:
    • 排名:第4136名
    • 原创:321篇
    • 转载:1篇
    • 译文:0篇
    • 评论:34条
    博客专栏
    最新评论