Skip to content

Commit 284ad4f

Browse files
refactor 856
1 parent b50e5df commit 284ad4f

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed
Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
11
package com.fishercoder.solutions;
22

3+
import java.util.Stack;
4+
35
public 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
}

src/test/java/com/fishercoder/_856Test.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import static org.junit.Assert.assertEquals;
99

10-
@Ignore
1110
public class _856Test {
1211
private static _856.Solution1 solution1;
1312

0 commit comments

Comments
 (0)