public class LongestValidParentheses {
public static int lvp(String s) {
if (s == null || s.length() <= 1) {
return 0;
}
char[] ca = s.toCharArray();
int[] dp = new int[s.length()+1];
int maxLen = 0;
// i for current location
for (int i = 2; i <= ca.length; i++) {
// 这里仅判断 ) 是因为 ( 无法判定一对括号的结束。 而 对于每个新出现的 ) 都进行处理的话,
// 问题就转化成了寻找前面出现过的一个合适位置的 ( 进行匹配
if (ca[i-1] == ')') {
// 存在以下3种可能
if (ca[i-2] == '(') {
// 1. 上一个字符是 ( ,那么和上一个字符就组成了一对括号
dp[i] = dp[i-2] + 2;
} else if (i-1 - dp[i-1] -1>=0 && s.charAt(i-1 - dp[i-1] -1) == '('){
// 2. 在往前找上一个位置最长匹配括号长度个字符后,