Skip to content

Commit 55f4859

Browse files
committed
feat: add solutions to lc problem: No.0856
No.0856.Score of Parentheses
1 parent a00675d commit 55f4859

File tree

9 files changed

+201
-17
lines changed

9 files changed

+201
-17
lines changed

solution/0800-0899/0811.Subdomain Visit Count/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ class Solution:
7272
def subdomainVisits(self, cpdomains: List[str]) -> List[str]:
7373
cnt = Counter()
7474
for s in cpdomains:
75-
v = int(s[:s.index(' ')])
75+
v = int(s[: s.index(' ')])
7676
for i, c in enumerate(s):
77-
if c in (' ', '.'):
78-
cnt[s[i + 1:]] += v
77+
if c in ' .':
78+
cnt[s[i + 1 :]] += v
7979
return [f'{v} {s}' for s, v in cnt.items()]
8080
```
8181

solution/0800-0899/0811.Subdomain Visit Count/README_EN.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ class Solution:
5555
def subdomainVisits(self, cpdomains: List[str]) -> List[str]:
5656
cnt = Counter()
5757
for s in cpdomains:
58-
v = int(s[:s.index(' ')])
58+
v = int(s[: s.index(' ')])
5959
for i, c in enumerate(s):
60-
if c in (' ', '.'):
61-
cnt[s[i + 1:]] += v
60+
if c in ' .':
61+
cnt[s[i + 1 :]] += v
6262
return [f'{v} {s}' for s, v in cnt.items()]
6363
```
6464

solution/0800-0899/0811.Subdomain Visit Count/Solution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ def subdomainVisits(self, cpdomains: List[str]) -> List[str]:
44
for s in cpdomains:
55
v = int(s[: s.index(' ')])
66
for i, c in enumerate(s):
7-
if c in (' ', '.'):
7+
if c in ' .':
88
cnt[s[i + 1 :]] += v
99
return [f'{v} {s}' for s, v in cnt.items()]

solution/0800-0899/0856.Score of Parentheses/README.md

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,98 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:计数**
57+
58+
我们通过观察发现,`()` 是唯一贡献分数的结构,外括号只是为该结构添加了一些乘数。所以我们只需要关心 `()`
59+
60+
我们用 $d$ 维护当前括号的深度,对于每个 `(`,我们将深度加一,对于每个 `)`,我们将深度减一。当我们遇到 `()` 时,我们将 $2^d$ 加到答案中。
61+
62+
我们举个实际的例子,以 `(()(()))` 为例,我们首先计算内部结构 `()(())` 的分数,然后将分数乘以 2。实际上,我们是在计算 `(()) + ((()))` 的分数。
63+
64+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 是字符串的长度。
65+
5666
<!-- tabs:start -->
5767

5868
### **Python3**
5969

6070
<!-- 这里可写当前语言的特殊实现逻辑 -->
6171

6272
```python
63-
73+
class Solution:
74+
def scoreOfParentheses(self, s: str) -> int:
75+
ans = d = 0
76+
for i, c in enumerate(s):
77+
if c == '(':
78+
d += 1
79+
else:
80+
d -= 1
81+
if s[i - 1] == '(':
82+
ans += 1 << d
83+
return ans
6484
```
6585

6686
### **Java**
6787

6888
<!-- 这里可写当前语言的特殊实现逻辑 -->
6989

7090
```java
91+
class Solution {
92+
public int scoreOfParentheses(String s) {
93+
int ans = 0, d = 0;
94+
for (int i = 0; i < s.length(); ++i) {
95+
if (s.charAt(i) == '(') {
96+
++d;
97+
} else {
98+
--d;
99+
if (s.charAt(i - 1) == '(') {
100+
ans += 1 << d;
101+
}
102+
}
103+
}
104+
return ans;
105+
}
106+
}
107+
```
108+
109+
### **C++**
110+
111+
```cpp
112+
class Solution {
113+
public:
114+
int scoreOfParentheses(string s) {
115+
int ans = 0, d = 0;
116+
for (int i = 0; i < s.size(); ++i) {
117+
if (s[i] == '(') {
118+
++d;
119+
} else {
120+
--d;
121+
if (s[i - 1] == '(') {
122+
ans += 1 << d;
123+
}
124+
}
125+
}
126+
return ans;
127+
}
128+
};
129+
```
71130
131+
### **Go**
132+
133+
```go
134+
func scoreOfParentheses(s string) int {
135+
ans, d := 0, 0
136+
for i, c := range s {
137+
if c == '(' {
138+
d++
139+
} else {
140+
d--
141+
if s[i-1] == '(' {
142+
ans += 1 << d
143+
}
144+
}
145+
}
146+
return ans
147+
}
72148
```
73149

74150
### **...**

solution/0800-0899/0856.Score of Parentheses/README_EN.md

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,79 @@
5252
### **Python3**
5353

5454
```python
55-
55+
class Solution:
56+
def scoreOfParentheses(self, s: str) -> int:
57+
ans = d = 0
58+
for i, c in enumerate(s):
59+
if c == '(':
60+
d += 1
61+
else:
62+
d -= 1
63+
if s[i - 1] == '(':
64+
ans += 1 << d
65+
return ans
5666
```
5767

5868
### **Java**
5969

6070
```java
71+
class Solution {
72+
public int scoreOfParentheses(String s) {
73+
int ans = 0, d = 0;
74+
for (int i = 0; i < s.length(); ++i) {
75+
if (s.charAt(i) == '(') {
76+
++d;
77+
} else {
78+
--d;
79+
if (s.charAt(i - 1) == '(') {
80+
ans += 1 << d;
81+
}
82+
}
83+
}
84+
return ans;
85+
}
86+
}
87+
```
88+
89+
### **C++**
90+
91+
```cpp
92+
class Solution {
93+
public:
94+
int scoreOfParentheses(string s) {
95+
int ans = 0, d = 0;
96+
for (int i = 0; i < s.size(); ++i) {
97+
if (s[i] == '(') {
98+
++d;
99+
} else {
100+
--d;
101+
if (s[i - 1] == '(') {
102+
ans += 1 << d;
103+
}
104+
}
105+
}
106+
return ans;
107+
}
108+
};
109+
```
61110
111+
### **Go**
112+
113+
```go
114+
func scoreOfParentheses(s string) int {
115+
ans, d := 0, 0
116+
for i, c := range s {
117+
if c == '(' {
118+
d++
119+
} else {
120+
d--
121+
if s[i-1] == '(' {
122+
ans += 1 << d
123+
}
124+
}
125+
}
126+
return ans
127+
}
62128
```
63129

64130
### **...**
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int scoreOfParentheses(string s) {
4+
int ans = 0, d = 0;
5+
for (int i = 0; i < s.size(); ++i) {
6+
if (s[i] == '(') {
7+
++d;
8+
} else {
9+
--d;
10+
if (s[i - 1] == '(') {
11+
ans += 1 << d;
12+
}
13+
}
14+
}
15+
return ans;
16+
}
17+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func scoreOfParentheses(s string) int {
2+
ans, d := 0, 0
3+
for i, c := range s {
4+
if c == '(' {
5+
d++
6+
} else {
7+
d--
8+
if s[i-1] == '(' {
9+
ans += 1 << d
10+
}
11+
}
12+
}
13+
return ans
14+
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
class Solution {
2-
public int scoreOfParentheses(String S) {
3-
int res = 0;
4-
for (int i = 0, d = 0; i < S.length(); ++i) {
5-
if (S.charAt(i) == '(') {
2+
public int scoreOfParentheses(String s) {
3+
int ans = 0, d = 0;
4+
for (int i = 0; i < s.length(); ++i) {
5+
if (s.charAt(i) == '(') {
66
++d;
77
} else {
88
--d;
9-
if (S.charAt(i - 1) == '(') {
10-
res += 1 << d;
9+
if (s.charAt(i - 1) == '(') {
10+
ans += 1 << d;
1111
}
1212
}
1313
}
14-
return res;
14+
return ans;
1515
}
16-
}
16+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def scoreOfParentheses(self, s: str) -> int:
3+
ans = d = 0
4+
for i, c in enumerate(s):
5+
if c == '(':
6+
d += 1
7+
else:
8+
d -= 1
9+
if s[i - 1] == '(':
10+
ans += 1 << d
11+
return ans

0 commit comments

Comments
 (0)