Skip to content

Commit 1340e22

Browse files
authored
Merge pull request larissalages#160 from ganm0/leetcode#32
Added solution to LeetCode#32 in C++
2 parents e34a83a + a87399b commit 1340e22

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

leetcode/cpp/Stacks/32.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//Longest valid parentheses
2+
3+
class Solution {
4+
public:
5+
int longestValidParentheses(string s) {
6+
stack<int> st;
7+
int len = 0;
8+
st.push(-1);
9+
for(int i=0;i<s.length();i++){
10+
if(s[i] == '('){
11+
st.push(i);
12+
}
13+
else{
14+
st.pop();
15+
if(!st.empty()){
16+
len=max(len,i - st.top());
17+
}
18+
else{
19+
st.push(i);
20+
}
21+
}
22+
}
23+
return len;
24+
}
25+
};

0 commit comments

Comments
 (0)