给你一个只包含 ‘(’ 和 ‘)’ 的字符串,找出最长有效(格式正确且连续)括号子串的长度。

class Solution {
public int longestValidParentheses(String s) {
Stack<Integer> st = new Stack<Integer>();
int ans = 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()) ans = Math.max(ans,i - start + 1);
else ans = Math.max(ans,i - st.peek());
}
else start = i + 1;
}
}
return ans;
}
}





![wsl [Ubuntu20.04.6] 安装 Hadoop](https://img-blog.csdnimg.cn/e5b8b090e7b24dc88bec46b2456b0d4e.png)


![[头歌]第1关:动态学生信息管理](https://img-blog.csdnimg.cn/4f8a17e574ff4a9c8f6c260a92c3943d.png)








![[PHP]Kodexplorer可道云 v4.47](https://img-blog.csdnimg.cn/532b538062ec407b85784c9884eef039.png)

