-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parenthese.java
32 lines (31 loc) · 966 Bytes
/
Parenthese.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
import java.util.LinkedList;
class Parenthese {
public static int longestValidParentheses(String s) {
int s_length = s.length();
int d[] = new int[s_length];
LinkedList<Integer> stack = new LinkedList<>();
int index = 0;
int max = 0;
for(int i=0;i<s_length;i++){
if(s.charAt(i)=='('){
stack.push(i);
}else{
if(stack.isEmpty()){
index = i+1;
}else{
int cur = stack.pop();
if(stack.isEmpty()){
max = Math.max(max, i-index+1);
}else{
max = Math.max(max,i-stack.peek()+1);
}
}
}
}
return max;
}
public static void main(String[] args) {
// System.out.println("test");
System.out.println(longestValidParentheses(")()())"));
}
}