File tree Expand file tree Collapse file tree 2 files changed +23
-3
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder Expand file tree Collapse file tree 2 files changed +23
-3
lines changed Original file line number Diff line number Diff line change 11package com .fishercoder .solutions ;
22
3+ import java .util .Stack ;
4+
35public class _856 {
46 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+ */
610 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 ;
829 }
930 }
1031}
Original file line number Diff line number Diff line change 77
88import static org .junit .Assert .assertEquals ;
99
10- @ Ignore
1110public class _856Test {
1211 private static _856 .Solution1 solution1 ;
1312
You can’t perform that action at this time.
0 commit comments