LeetCode #32

Longest Valid Parentheses

1개의 풀이 · C++

문제 원문 보기 ↗

SOLUTION INFO

C++ · main.cpp

main.cpp
class Solution {
public:
    int longestValidParentheses(string s) {
        int N = (int)s.size();
        stack<int> st; st.push(-1);
        int mx = 0;
        for(int i = 0; i < N; ++i) {
            char ch = s[i];
            if(st.top() != -1 && s[st.top()] == '(' && ch == ')') {
                st.pop(), mx = max(mx, i - st.top());
            }
            else st.push(i);
        }
        return mx;
    }
};

SOLUTION DESCRIPTION

풀이 설명

등록된 풀이 설명이 없습니다.