给定一个字符串,只包含‘(',')',查找给定字符串中最长的有效'(',')'长度
class Solution {
public int longestValidParentheses(String s) {
Stack<Integer> stack = new Stack<>();
int max = 0,left = -1;
for(int i=0;i<s.length();i++){
if(s.charAt(i) == '(') stack.push(i);
else{
if(stack.empty()) left = i;
else{
stack.pop();
if(stack.empty()) max = Math.max(max,i - left);
else{
max = Math.max(max,i - stack.peek());
}
}
}
}
return max;
}
}