艾恩凝
个人博客 https://aeneag.xyz/
公众号 技术乱舞
每日一练,保持手感
2021/10/19
题目
https://leetcode-cn.com/problems/longest-valid-parentheses/
题解
class Solution {
public:
int longestValidParentheses(string s) {
int longest_len = 0 ;
stack<int> l_stack;
l_stack.push(-1);
for(int i = 0 ; i < s.size() ; ++i){
if(s[i] == '('){
l_stack.push(i);
}else{
l_stack.pop();
if(l_stack.empty()){
l_stack.push(i);
}else{
longest_len = max(longest_len,i-l_stack.top());
}
}
}
return longest_len;
}
};
欢迎关注 #公众号:技术乱舞 一起交流
灵魂碰撞