public static int longestValidParentheses(String s) {

    int max = 0;
    int start = 0;

    Stack<Integer> stack = new Stack<Integer>();

    for (int i = 0 ; i < s.length() ; i++) {
      char ch = s.charAt(i);

      if (ch == '(') {
        stack.push(i);
      } else {
        if (stack.isEmpty()) {
          start = i + 1;
        } else {
          // 好像挺有道理....
          stack.pop();
          if (stack.isEmpty()) {
            max = Math.max(max, i - start + 1);
          } else {
            max = Math.max(max, i - stack.peek());
          }
        }
      }
    }


    return max;

  }