Skip to content

Commit ff3f47a

Browse files
committed
nothing done
1 parent 00719e3 commit ff3f47a

4 files changed

Lines changed: 27 additions & 9 deletions

File tree

384 Bytes
Binary file not shown.
440 Bytes
Binary file not shown.

src/Class24/LongestValidParentheses.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package Class24;
22

3+
import java.util.Deque;
4+
import java.util.LinkedList;
5+
36
/*
47
* Given a string containing just the characters '(' and ')', find the length of the
58
* longest valid (well-formed) parentheses substring.
@@ -15,19 +18,24 @@ public int longestValidParentheses(String s) {
1518
return 0;
1619
}
1720
int[] len = new int[s.length()];
21+
Deque<Character> stack = new LinkedList<Character>();
1822
// base case:
23+
len[0] = 0;
24+
stack.offerFirst(s.charAt(0));
1925
// len[1] = s.charAt(0) == '(' && s.charAt(1) == ')' ? 1 : 0;
20-
int left = 0;
21-
int right = 0;
22-
for (int i = 0; i < s.length(); i++) {
26+
// int left = 0;
27+
// int right = 0;
28+
for (int i = 1; i < s.length(); i++) {
2329
if (s.charAt(i) == '(') {
24-
left++;
25-
} else if (left > right && s.charAt(i) == ')') {
26-
right++;
27-
} else
28-
len[i] = 0;
30+
stack.offerFirst('(');
31+
len[i] = len[i-1];
32+
} else {
33+
if (s.charAt(i) == ')' && stack.peekFirst() == '(') {
34+
len[i] = len[i - 1] + 1;
35+
}
36+
}
2937
}
30-
return right * 2;
38+
return len[s.length() - 1];
3139
}
3240

3341
public static void main(String[] args) {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package Class27;
2+
3+
public class LongestAscendingSubsequence {
4+
5+
public static void main(String[] args) {
6+
// TODO Auto-generated method stub
7+
8+
}
9+
10+
}

0 commit comments

Comments
 (0)