Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit284ad4f

Browse files
refactor 856
1 parentb50e5df commit284ad4f

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
packagecom.fishercoder.solutions;
22

3+
importjava.util.Stack;
4+
35
publicclass_856 {
46
publicstaticclassSolution1 {
5-
//TODO: implement it
7+
/**
8+
* credit: https://leetcode.com/problems/score-of-parentheses/discuss/141763/Java-solution-using-Stack
9+
*/
610
publicintscoreOfParentheses(StringS) {
7-
return -1;
11+
Stack<Integer>stack =newStack<>();
12+
for (inti =0;i <S.length();i++) {
13+
if (S.charAt(i) =='(') {
14+
stack.push(-1);
15+
}else {
16+
intcurr =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+
intscore =0;
25+
while (!stack.isEmpty()) {
26+
score +=stack.pop();
27+
}
28+
returnscore;
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
importstaticorg.junit.Assert.assertEquals;
99

10-
@Ignore
1110
publicclass_856Test {
1211
privatestatic_856.Solution1solution1;
1312

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp