32. Longest Valid Parentheses (Stack; DP)

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.

法I:把所有invalid的括号位置都标记出来,比较invalid之间的长度哪段最长

class Solution {
public:
    int longestValidParentheses(string s) {
        vector<int> invalidPos;
        invalidPos.push_back(-1);
        invalidPos.push_back(s.length());
        stack<int> lParenPos;
        int len = 0, ret = 0;

        for(int i = 0; i < s.length(); i++){
            if(s[i]==‘(‘){
                lParenPos.push(i);
            }
            else{ //right parenthese
                if(lParenPos.empty()){
                    invalidPos.push_back(i);
                }
                else{
                    lParenPos.pop();
                }
            }
        }

        while(!lParenPos.empty()){
            invalidPos.push_back(lParenPos.top());
            lParenPos.pop();
        }

        sort(invalidPos.begin(), invalidPos.end());
        for(int i = 1; i < invalidPos.size(); i++){
            len = invalidPos[i]-invalidPos[i-1]-1;
            if(len > ret) ret = len;
        }

        return ret;
    }
};

法II:动态规划

class Solution {
public:
    int longestValidParentheses(string s) {
        if(s.empty()) return 0;
        stack<int> leftStack;
        int ret = 0;
        int currentMax = 0;
        int leftPos;
        vector<int> dp(s.length()+1,0); //currentMax无法检测到连续valid的情况,eg: ()(), 所以需要动态规划记录i位置之前连续多少个valid。

        for(int i = 0; i <s.length(); i++){
            if(s[i]==‘)‘){
                if(leftStack.empty()){
                    currentMax = 0;
                }
                else
                {
                    leftPos = leftStack.top();
                    leftStack.pop();
                    currentMax = i-leftPos+1 + dp[leftPos];
                    dp[i+1] = currentMax;
                    ret = max(ret,currentMax);
                }
            }
            else{
                leftStack.push(i); //push the index of ‘(‘
            }
        }
        return ret;
    }
};
时间: 2024-08-10 13:07:57

32. Longest Valid Parentheses (Stack; DP)的相关文章

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

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

刷题32. Longest Valid Parentheses

一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过5次提交终于正确了. 性能如下: Runtime: 8 ms, faster than 61.76% of C++ online submissions for Longest Valid Parentheses. Memory Usage: 9.8 MB, less than 10.71% of

LeetCode - 32. Longest Valid Parentheses

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

[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. Example 1:             Input: "(()"                         Output: 2                      Explanation: The long

leetCode 32.Longest Valid Parentheses (有效的最大括号) 解题思路和方法

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

LeetCode 32 Longest Valid Parentheses (C,C++,Java,Python)

Problem: 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 之 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

[leedcode 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 is &