文章

99

粉丝

120

获赞

8

访问

75.1k

头像
最长有效括号 32
备考心情
发布于2024年9月5日 15:03
阅读数 701

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

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发