File tree 2 files changed +23
-3
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder
2 files changed +23
-3
lines changed Original file line number Diff line number Diff line change 1
1
package com .fishercoder .solutions ;
2
2
3
+ import java .util .Stack ;
4
+
3
5
public class _856 {
4
6
public static class Solution1 {
5
- //TODO: implement it
7
+ /**
8
+ * credit: https://leetcode.com/problems/score-of-parentheses/discuss/141763/Java-solution-using-Stack
9
+ */
6
10
public int scoreOfParentheses (String S ) {
7
- return -1 ;
11
+ Stack <Integer > stack = new Stack <>();
12
+ for (int i = 0 ; i < S .length (); i ++) {
13
+ if (S .charAt (i ) == '(' ) {
14
+ stack .push (-1 );
15
+ } else {
16
+ int curr = 0 ;
17
+ while (stack .peek () != -1 ) {
18
+ curr += stack .pop ();
19
+ }
20
+ stack .pop ();
21
+ stack .push (curr == 0 ? 1 : curr * 2 );
22
+ }
23
+ }
24
+ int score = 0 ;
25
+ while (!stack .isEmpty ()) {
26
+ score += stack .pop ();
27
+ }
28
+ return score ;
8
29
}
9
30
}
10
31
}
Original file line number Diff line number Diff line change 7
7
8
8
import static org .junit .Assert .assertEquals ;
9
9
10
- @ Ignore
11
10
public class _856Test {
12
11
private static _856 .Solution1 solution1 ;
13
12
You can’t perform that action at this time.
0 commit comments