int longestValidParentheses(string s){
stack<int> st;
st.push(-1);// 初始化基准
int ans =0;for(int i =0; i < s.size(); i++){if(s[i]=='('){
st.push(i);}else{
st.pop();if(st.empty()){// 栈空了,需要设置新基准
st.push(i);}else{
ans =max(ans, i - st.top());// 计算的是距离}}}return ans;}
Loading Comments...