Skip to content

Commit cbfd962

Browse files
committed
最长连续匹配括号数
1 parent c1e5cab commit cbfd962

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

LongestValidParentheses.java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
public class Solution {
2+
public int longestValidParentheses(String s) {
3+
if(s==null||s.length()==0) {
4+
return 0;
5+
}
6+
int start = -1;
7+
int maxLength = 0;
8+
Stack stack = new Stack();
9+
for(int i=0;i<s.length();i++) {
10+
if(s.charAt(i)=='(') {
11+
stack.push(i);
12+
} else {
13+
if(!stack.empty()) {
14+
stack.pop();
15+
if(stack.empty()==true) {
16+
maxLength = Math.max(i - start , maxLength);
17+
} else {
18+
maxLength = Math.max(i - (int)stack.peek() , maxLength);
19+
}
20+
} else {
21+
start = i;
22+
}
23+
}
24+
}
25+
26+
return maxLength;
27+
}
28+
}

0 commit comments

Comments
 (0)