Skip to content

Commit 771e9e3

Browse files
committed
一刷856
1 parent fc9b1a6 commit 771e9e3

6 files changed

Lines changed: 98 additions & 45 deletions

File tree

Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,83 @@
11
[#0856-score-of-parentheses]
2-
= 856. Score of Parentheses
2+
= 856. 括号的分数
33

4-
{leetcode}/problems/score-of-parentheses/[LeetCode - Score of Parentheses^]
4+
https://leetcode.cn/problems/score-of-parentheses/[LeetCode - 856. 括号的分数^]
55

6-
Given a balanced parentheses string `S`, compute the score of the string based on the following rule:
6+
给定一个平衡括号字符串 `S`,按下述规则计算该字符串的分数:
77

8+
* `()` 得 1 分。
9+
* `AB``A + B` 分,其中 A 和 B 是平衡括号字符串。
10+
* `(A)``2 * A` 分,其中 A 是平衡括号字符串。
811
9-
* `()` has score 1
10-
* `AB` has score `A + B`, where A and B are balanced parentheses strings.
11-
* `(A)` has score `2 * A`, where A is a balanced parentheses string.
12+
*示例 1:*
1213

14+
....
15+
输入: "()"
16+
输出: 1
17+
....
1318

14-
19+
*示例 2:*
1520

21+
....
22+
输入: "(())"
23+
输出: 2
24+
....
1625

17-
*Example 1:*
26+
*示例 3:*
1827

19-
[subs="verbatim,quotes,macros"]
20-
----
21-
*Input:* "()"
22-
*Output:* 1
23-
----
24-
25-
26-
*Example 2:*
27-
28-
[subs="verbatim,quotes,macros"]
29-
----
30-
*Input:* "(())"
31-
*Output:* 2
32-
----
33-
34-
35-
*Example 3:*
36-
37-
[subs="verbatim,quotes,macros"]
38-
----
39-
*Input:* "()()"
40-
*Output:* 2
41-
----
42-
43-
44-
*Example 4:*
45-
46-
[subs="verbatim,quotes,macros"]
47-
----
48-
*Input:* "(()(()))"
49-
*Output:* 6
50-
----
28+
....
29+
输入: "()()"
30+
输出: 2
31+
....
5132

52-
33+
*示例 4:*
5334

54-
*Note:*
35+
....
36+
输入: "(()(()))"
37+
输出: 6
38+
....
5539

40+
*提示:*
5641

57-
* `S` is a balanced parentheses string, containing only `(` and `)`.
58-
* `2 <= S.length <= 50`
42+
. `S` 是平衡括号字符串,且只含有 `(` `)`
43+
. `2 \<= S.length \<= 50`
5944

6045

6146

47+
== 思路分析
6248

49+
最简单直接的解法是:栈。
6350

51+
image::images/0856-10.png[{image_attr}]
6452

53+
image::images/0856-11.png[{image_attr}]
6554

55+
image::images/0856-12.png[{image_attr}]
6656

6757
[[src-0856]]
58+
[tabs]
59+
====
60+
一刷::
61+
+
62+
--
6863
[{java_src_attr}]
6964
----
7065
include::{sourcedir}/_0856_ScoreOfParentheses.java[tag=answer]
7166
----
67+
--
68+
69+
// 二刷::
70+
// +
71+
// --
72+
// [{java_src_attr}]
73+
// ----
74+
// include::{sourcedir}/_0856_ScoreOfParentheses_2.java[tag=answer]
75+
// ----
76+
// --
77+
====
78+
79+
80+
== 参考资料
7281

82+
. https://leetcode.cn/problems/score-of-parentheses/solutions/1876173/gua-hao-de-fen-shu-by-leetcode-solution-we6b/[856. 括号的分数 - 官方题解^]
83+
. https://leetcode.cn/problems/score-of-parentheses/solutions/1878748/zhua-wa-mou-si-by-muse-77-hy72/[856. 括号的分数 - 图解LeetCode^]

docs/images/0856-10.png

79.9 KB
Loading

docs/images/0856-11.png

75.4 KB
Loading

docs/images/0856-12.png

108 KB
Loading

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1804,7 +1804,7 @@ include::0853-car-fleet.adoc[leveloffset=+1]
18041804

18051805
include::0855-exam-room.adoc[leveloffset=+1]
18061806

1807-
// include::0856-score-of-parentheses.adoc[leveloffset=+1]
1807+
include::0856-score-of-parentheses.adoc[leveloffset=+1]
18081808

18091809
// include::0857-minimum-cost-to-hire-k-workers.adoc[leveloffset=+1]
18101810

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
5+
6+
public class _0856_ScoreOfParentheses {
7+
// tag::answer[]
8+
/**
9+
* @author D瓜哥 · https://www.diguage.com
10+
* @since 2026-08-10 22:08:50
11+
*/
12+
public int scoreOfParentheses(String s) {
13+
Deque<String> stack = new ArrayDeque<>();
14+
for (char p : s.toCharArray()) {
15+
if (p == '(') {
16+
stack.push(String.valueOf(p));
17+
} else {
18+
if ("(".equals(stack.peek())) {
19+
stack.pop();
20+
stack.push("1");
21+
} else {
22+
int temp = 0;
23+
while (!"(".equals(stack.peek())) {
24+
temp += Integer.parseInt(stack.pop());
25+
}
26+
stack.poll();
27+
stack.push(String.valueOf(2 * temp));
28+
}
29+
}
30+
}
31+
int result = 0;
32+
while (!stack.isEmpty()) {
33+
result += Integer.parseInt(stack.pop());
34+
}
35+
return result;
36+
}
37+
// end::answer[]
38+
static void main() {
39+
new _0856_ScoreOfParentheses()
40+
.scoreOfParentheses("(()(()))");
41+
}
42+
}

0 commit comments

Comments
 (0)