-
Notifications
You must be signed in to change notification settings - Fork 13
/
32.最长有效括号.java
97 lines (92 loc) · 2.6 KB
/
32.最长有效括号.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
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
88
89
90
91
92
93
94
95
96
97
给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度。
示例 1:
输入: "(()"
输出: 2
解释: 最长有效括号子串为 "()"
示例 2:
输入: ")()())"
输出: 4
解释: 最长有效括号子串为 "()()"
// 方法一 暴力
// time:O(N^2)
// space:O(N)
class Solution {
public boolean isValid(String s){
Stack<Character> stack = new Stack<Character>();
for (int i =0;i<s.length();i++){
if (s.charAt(i)=='('){
stack.push('(');
}else if (!stack.empty() && stack.peek() == '('){
stack.pop();
}
else{
return false;
}
}
return stack.empty();
}
public int longestValidParentheses(String s) {
int maxlen = 0;
for (int i = 0;i<s.length();i++){
for (int j = i + 2;j<=s.length();j+=2){
if (isValid(s.substring(i,j))){
maxlen = Math.max(maxlen,j-i);
}
}
}
return maxlen;
}
}
// 方法二 栈
class Solution {
public int longestValidParentheses(String s) {
int maxans = 0;
Stack<Integer> stack = new Stack<>();
stack.push(-1);
for (int i = 0;i < s.length();i++){
if (s.charAt(i)== '('){
stack.push(i);
}else{
stack.pop();
if (stack.empty()){
stack.push(i);
}else{
maxans = Math.max(maxans,i-stack.peek());
}
}
}
return maxans;
}
}
// 方法三 不需要额外空间
class Solution {
public int longestValidParentheses(String s) {
int left = 0,right = 0,maxlength = 0;
for (int i = 0;i < s.length();i++){
if (s.charAt(i) == '('){
left++;
}else{
right++;
}
if (left == right){
maxlength = Math.max(maxlength,2*right);
}else if(right > left){
left = right = 0;
}
}
left = right = 0;
for (int i = s.length() - 1;i >= 0; i--){
if (s.charAt(i) == '('){
left ++;
}else{
right++;
}
if(left == right){
maxlength = Math.max(maxlength,2*left);
}else if (left >= right){
left = right = 0;
}
}
return maxlength;
}
}