public int longestValidParentheses_2(String s) {
        // Note: The Solution object is instantiated only once and is reused by
        // each test case.
        Stack<Integer> stack = new Stack<Integer>();
        int max = 0;
        int count = 0;
        for(int i = 0; i < s.length(); ++i){
            if(s.charAt(i) == '('){
                stack.push(count);
                count = 0;
            }else if(stack.size() > 0){
                    count += 2 + stack.pop();
                    max = Math.max(max, count);
            }else {
                count = 0;
            }
        }
        return max;
    }