-
Notifications
You must be signed in to change notification settings - Fork 0
/
LongestValidParentheses.java
52 lines (47 loc) · 1.21 KB
/
LongestValidParentheses.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
*
* @author chenqun Given a string containing just the characters '(' and ')',
* find the length of the longest valid (well-formed) parentheses
* substring. For "(()", the longest valid parentheses substring is
* "()", which has length = 2. Another example is ")()())", where the
* longest valid parentheses substring is "()()", which has length = 4.
*
*/
//动态规划,找到每个(对应的)
public class LongestValidParentheses {
public int longestValidParentheses(String s) {
int length = s.length();
s = s + "#";
int[] id = new int[s.length()];
for (int i = 0; i < id.length; i++) {
id[i] = -1;
}
if (s.length() <= 1)
return 0;
for (int i = length - 1; i >= 0; i--) {
if (s.charAt(i) == '(') {
int j = i + 1;
while (s.charAt(j) == '(' && id[j] != -1)
j = id[j] + 1;
if (s.charAt(j) == ')')
id[i] = j;
}
}
int longest = 0;
int cur = 0;
for (int i = 0; i < length + 1;) {
if (id[i] == -1) {
longest = Math.max(cur, longest);
cur = 0;
i = i + 1;
} else {
cur = cur + id[i] - i + 1;
i = id[i] + 1;
}
}
return longest;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}