Longest Valid Parentheses

给一个字符串, 找到其中最长的括号. 因为要找括号, 所以要用stack存左括号的index, 然后用循环去尽量往右找右括号.

public class Solution {
    public int longestValidParentheses(String s) {
        Stack<Integer> st = new Stack<>();
        int max = 0;
        for(int i = 0 ; i < s.length(); i++) {
            if(s.charAt(i) == ')' && !st.isEmpty() && s.charAt(st.peek()) == '('){
                st.pop();
                max = Math.max(max, i - (st.isEmpty()? -1 : st.peek()));
            }
            else
                st.push(i);
        }
        return max;
    }
}