一维DP
划分型:f[] means XXXX ending with i
Longest Valid Parentheses
public int longestValidParentheses(String s) {
if (s == null || s.length() == 0) return 0;
int n = s.length();
int[] f = new int[n];
int max = 0;
for (int i = 1; i < n; i++) {
if (s.charAt(i) == ')') {
if (s.charAt(i - 1) == '(') { //“()”
f[i] = 2 + (i >= 2 ? f[i - 2] : 0);
} else if (i - f[i-1] - 1 >= 0 && s.charAt(i - f[i-1] - 1) == '(') {
f[i] = 2 + f[i-1] + (i - f[i-1] - 2 >= 0 ? f[i - f[i-1] - 2] : 0);
}
}
max = Math.max(max, f[i]);
}
return max;
}Decode Ways
Last updated