-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
31 lines (27 loc) · 762 Bytes
/
Solution.java
File metadata and controls
31 lines (27 loc) · 762 Bytes
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
package leetCode_32;
import java.util.Stack;
/**
* @author dimdark
* @date 2017-10-01
* @time 7:40 PM
*/
public class Solution {
public int longestValidParentheses(String s) {
if (s == null) return 0;
Stack<Integer> stk = new Stack<Integer>();
int maxLen = 0, leftBound = -1, len = s.length();
for (int i = 0; i < len; ++i) {
if (s.charAt(i) == '(') {
stk.push(i);
} else {
if (!stk.empty()) {
stk.pop();
maxLen = Integer.max(maxLen, i - (stk.empty() ? leftBound : stk.peek()));
} else {
leftBound = i; // update
}
}
}
return maxLen;
}
}