dp篇
栈。
好简单,通俗易懂。
class Solution {
public int longestValidParentheses(String s) {
Stack<Integer>st=new Stack<Integer>();
int ret=0;
for(int i=0,start=0;i<s.length();i++){
if(s.charAt(i)=='(') st.add(i);
else{
if(!st.isEmpty()){
st.pop();
if(st.isEmpty()) ret=Math.max(ret,i-start+1);
else ret=Math.max(ret,i-st.peek());
}
else start=i+1;
}
}
return ret;
}
}
ps: