Public int longestValidParentheses(String s) {char[] arr = s.tochararray (); char[] reverseArr = new char[arr.length];for (int i = arr.length - 1, k = 0; i >= 0; )
reverseArr[k++] = arr[i--];
for(int i = 0; i < reverseArr.length; ReverseArr [I] ^= 1; reverseArr[I] ^= 1;returnMath.max(helper(arr), helper(reverseArr)); Private int helper(char[] array) {int res = 0; private int helper(char[] array) {int res = 0; private int helper(char[] array) {int res = 0;for (int i = 0, start = 0, cnt = 0; i < array.length; i++) {
if (array[i] == '(') {
cnt++;
} else {
cnt--;
if (cnt < 0) {
start = i + 1;
cnt = 0;
} else if(cnt == 0) { res = Math.max(res, i - start + 1); }}}returnres; }}Copy the code