-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode32.java
More file actions
87 lines (75 loc) · 2.48 KB
/
LeetCode32.java
File metadata and controls
87 lines (75 loc) · 2.48 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import java.util.Stack;
public class LeetCode32 {
public static void main(String[] args) {
// 输入:s = "(()"
// 输出:2
System.out.println(new Solution32_2().longestValidParentheses("(()"));
// 输入:s = ")()())"
// 输出:4
System.out.println(new Solution32_2().longestValidParentheses(")()())"));
// 输入:s = ""
// 输出:0
System.out.println(new Solution32_2().longestValidParentheses(""));
// 输入:s = "()()"
// 输出:4
System.out.println(new Solution32_2().longestValidParentheses("()()"));
// 输入:s = "()(())"
// 输出:6
System.out.println(new Solution32_2().longestValidParentheses("()(())"));
}
}
class Solution32_1 {
// NOTE: 暴力解法,部分超时
public int longestValidParentheses(String s) {
if (s.length() == 0) {
return 0;
}
for (int length = s.length() % 2 == 0 ? s.length() : s.length() - 1; length >= 2; length -= 2) {
for (int i = 0; i + length <= s.length(); i++) {
if (isValidParentheses(s, i, i + length - 1)) {
return length;
}
}
}
return 0;
}
public boolean isValidParentheses(String s, int start, int end) {
Stack<Character> stack = new Stack<Character>();
for (int i = start; i <= end; i++) {
if (s.charAt(i) == '(') {
stack.push('(');
} else {
if (stack.isEmpty()) {
return false;
}
stack.pop();
}
}
return stack.isEmpty();
}
}
class Solution32_2 {
public int longestValidParentheses(String s) {
if (s.length() < 2) {
return 0;
}
int max = 0;
int[] dp = new int[s.length()];
for (int end = 1; end < s.length(); end++) {
if (s.charAt(end) == '(') {
continue;
}
if (s.charAt(end - 1) == '(') {
dp[end] = end - 2 >= 0 ? dp[end - 2] + 2 : 2;
} else {
int ind = end - 1 - dp[end - 1];
if (ind >= 0 && s.charAt(ind) == '(') {
dp[end] = dp[end - 1] + 2 + (ind - 1 >= 0 ? dp[ind - 1] : 0);
}
}
// System.out.println(end + " " + dp[end]);
max = Math.max(max, dp[end]);
}
return max;
}
}