Skip to content

Commit

Permalink
Solve: Longest Valid Parentheses
Browse files Browse the repository at this point in the history
  • Loading branch information
fkdl0048 committed Oct 5, 2024
1 parent c0969b6 commit 9b7ba7d
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions LeetCode/Longest Valid Parentheses.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public:
int longestValidParentheses(string s) {
stack<int> st;
int result = 0;
st.push(-1);

for (int i = 0; i < s.length(); i++) {
if (s[i] == '(') {
st.push(i);
} else {
st.pop();
if (st.empty()) {
st.push(i);
} else {
result = max(result, i - st.top());
}
}
}

return result;
}
};

0 comments on commit 9b7ba7d

Please sign in to comment.