We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c1e5cab commit cbfd962Copy full SHA for cbfd962
LongestValidParentheses.java
@@ -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
18
+ maxLength = Math.max(i - (int)stack.peek() , maxLength);
19
20
21
+ start = i;
22
23
24
25
+
26
+ return maxLength;
27
28
+}
0 commit comments