public int longestValidParentheses(String s) {
        Stack<Character> stack = new Stack<Character>();      //保存括号
        Stack<Integer> index = new Stack<Integer>();          //保存下标
        int val[] = new int[s.length()];                     //标记能匹配的括号
        int num =0;
        for(int i =0 ;i< s.length();i++){
            if(s.charAt(i) == ')'&& !stack.empty() && stack.peek() == '('){
                stack.pop();           // 如果括号匹配，记录下标
                val[index.pop()] = 1;
                val[i] = 1;
            }else{
                stack.push(s.charAt(i));
                index.push(i);
            }
        }
        int k =0;
        for(int i =0 ;i< s.length();i++){ // 统计 最长连续有效括号数量
            if(val[i] == 0 ){
                num = Math.max(k, num);
                k=0;
            }
            else
                k++;
        }
        num = Math.max(k, num);
        return num;
    }