https://leetcode.com/problems/longest-valid-parentheses/description/
解题思路:用stack来解决
class Solution {
public int longestValidParentheses(String s) {
Stack<Integer> stack = new Stack<Integer>();
int res = 0;
int count = 0;
for (int i = 0 ; i < s.length(); i++){
if(s.charAt(i)=='('){
stack.push(count);
count = 0;
}else{
if(!stack.isEmpty()){
count = count + 1 + stack.peek();
stack.pop();
res = Math.max(res, count);
}else{
count = 0;
}
}
}
return res*2;
}
}
网友评论