[Leetcode] Longest Valid Parentheses

本文探讨了如何寻找字符串中最长的有效括号子串长度的问题。通过两种不同的C++实现方式,介绍了使用栈来解决这一经典算法问题的方法,并分享了一次成功通过面试的经历。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Longest Valid ParenthesesMar 1 '125700 / 20657

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.

 

之前大摩面试时,问到了这题,当时愚昧了。

一开始,写错了。

后来,想了个N^2的方法。 T_T。

不过好歹这次一次AC :)

struct A {
    A(char cc, int vv=0) : c(cc), v(vv) {}
    char c;
    int v;
};
class Solution {
public:
    
    int longestValidParentheses(string s) {
        int len = s.size();
        if (len == 0) return 0;
        stack<A> st;
        st.push(A(s[0]));
        int cnt = 0;
        for (int i = 1; i < len; i++) {
            cnt = 0;
            if (s[i] == '(') st.push(A(s[i]));
            else {
                while (!st.empty() && st.top().c == '#') {
                    cnt += st.top().v;
                    st.pop();
                }
                
                if (!st.empty() && st.top().c == '(') {
                    cnt += 2;
                    st.pop();
                    st.push(A('#', cnt));
                }
                else {
                    // top().c == '#'
                    if (cnt > 0) st.push(A('#',cnt));
                    st.push(A(')'));
                }
            }
        }
        
        cnt = 0;
        int mx = 0;
        while (!st.empty()) {
            if (st.top().c == '#') {
                cnt += st.top().v;
                if (mx < cnt) mx = cnt;
            } else {
                cnt = 0;
            }
            st.pop();
        }
        return mx;
    }
};

 

 

class Solution {
public:
    int longestValidParentheses(string s) {
        int n = s.size();
        if (n == 0) return 0;
        int i = 0;
        stack<pair<char, int> > st;
        st.push(make_pair('#', 0));
        st.push(make_pair(s[i++], 0));
        while (!st.empty() && i < n) {
            if (s[i] == '(') st.push(make_pair(s[i++], 0));
            else {
                i++;
                int cnt = 0;
                while (st.top().first == '.') cnt += st.top().second, st.pop();
                if (st.top().first == '(') {
                    st.pop();
                    st.push(make_pair('.', cnt + 1));
                }
                else {
                    if (cnt > 0) st.push(make_pair('.', cnt));
                    st.push(make_pair(')', 0));
                }
            }
        }
        int res = 0;
        while (st.top().first != '#') {
            int cnt = 0;
            while (st.top().first == '.') cnt += st.top().second, st.pop();
            res = max(res, cnt);
            while (st.top().first == '(' || st.top().first == ')') st.pop();
        }
        return res*2;
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值