public int longestValidParentheses(String s) {
        if(s == null || s.length() == 0) return 0;
        int[] res = new int[s.length()];
        int max = 0;
        Stack<Integer> stack = new Stack<Integer>();
        
        for(int i = 0; i < s.length(); i++) {
            if(s.charAt(i) == '(') {
                stack.add(i);
            } else {
                if(!stack.isEmpty()) {
                    int pre = stack.pop();
                    res[i] = (pre == 0 ? 0 : res[pre-1]) + i - pre + 1; // bug, use 2 at first...
                    if(res[i] > max) max = res[i];
                }
            }
        }
        
        return max;
    }