32. 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 longest valid parentheses substring is “()”
Example 2:

Input: “)()())”
Output: 4
Explanation: The longest valid parentheses substring is “()()”

三种方法,一种是用栈的方法,一种是两遍扫描,还有一种是DP
栈:

class Solution {
public:
int longestValidParentheses(const string& s) {
    stack<int> stack;
    int answer = 0;
    for(int i=0;i<s.size();i++){
        if(s[i]=='(') stack.push(i);
        else{
            if(!stack.empty() && s[stack.top()]=='('){  
                stack.pop();//stack.top存的是上一次s[i]  (())
                int last = -1;
                if(!stack.empty()){
                    last = stack.top();
                }
                int curLen = i - last;
                answer = max(curLen,answer);
            }
            else{
                stack.push(i);
            }
        }
    }
    return answer;
    }
};

两遍扫描 原因 如果一遍 (()这样的会出错,此时从后往前再扫描一次就行

class Solution {
public:
int longestValidParentheses(const string& s) {
    int answer = 0, depth = 0, start = -1;
        for (int i = 0; i < s.size(); ++i) {
            if (s[i] == '(') {
                ++depth;
                } 
            else {
                --depth;
                if (depth < 0) {
                    start = i;
                    depth = 0;
                } 
                else if (depth == 0) {
                    answer = max(answer, i - start);
                }
            }
        }
    start = s.size();
    depth = 0;
    for(int i=s.size()-1;i>=0;i--){
        if(s[i]==')')
            depth++;
        else{
            depth--;
            if(depth<0){
                start = i;
                depth = 0;
            }
            else if(depth==0){
                answer =  max(answer,start-i);
            }
        }
    }
    return answer;
    }
};

DP的方法 我的弱点

  int longestValidParentheses(string s) {
            if(s.length() <= 1) return 0;
            int curMax = 0;
            vector<int> longest(s.size(),0);
            for(int i=1; i < s.length(); i++){
                if(s[i] == ')'){
                    if(s[i-1] == '('){
                        longest[i] = (i-2) >= 0 ? (longest[i-2] + 2) : 2;
                        curMax = max(longest[i],curMax);
                    }
                    else{ // if s[i-1] == ')', combine the previous length.
                        if(i-longest[i-1]-1 >= 0 && s[i-longest[i-1]-1] == '('){
                            longest[i] = longest[i-1] + 2 + ((i-longest[i-1]-2 >= 0)?longest[i-longest[i-1]-2]:0);
                            curMax = max(longest[i],curMax);
                        }
                    }
                }
                //else if s[i] == '(', skip it, because longest[i] must be 0
            }
            return curMax;
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值