Skip to content

Commit aea3072

Browse files
committed
feat: add solutions to lc problem: No.1021
No.1021.Remove Outermost Parentheses
1 parent cf06c76 commit aea3072

File tree

5 files changed

+221
-47
lines changed

5 files changed

+221
-47
lines changed

solution/1000-1099/1021.Remove Outermost Parentheses/README.md

Lines changed: 99 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@
6363

6464
<!-- 这里可写通用的实现逻辑 -->
6565

66+
**方法一:计数**
67+
68+
遍历字符串,遇到左括号 `(` 计数器加一,此时计数器不为 1 时,说明当前括号不是最外层括号,将其加入结果字符串。遇到右括号 `)` 计数器减一,此时计数器不为 0 时,说明当前括号不是最外层括号,将其加入结果字符串。
69+
70+
时间复杂度 $O(n)$,忽略答案字符串的空间开销,空间复杂度 $O(1)$。其中 $n$ 为字符串长度。
71+
6672
<!-- tabs:start -->
6773

6874
### **Python3**
@@ -86,27 +92,65 @@ class Solution:
8692
return ''.join(ans)
8793
```
8894

95+
```python
96+
class Solution:
97+
def removeOuterParentheses(self, s: str) -> str:
98+
ans = []
99+
cnt = 0
100+
for c in s:
101+
if c == '(':
102+
cnt += 1
103+
if cnt > 1:
104+
ans.append(c)
105+
if c == ')':
106+
cnt -= 1
107+
return ''.join(ans)
108+
```
109+
89110
### **Java**
90111

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

93114
```java
94115
class Solution {
95-
public String removeOuterParentheses(String S) {
96-
StringBuilder res = new StringBuilder();
116+
public String removeOuterParentheses(String s) {
117+
StringBuilder ans = new StringBuilder();
97118
int cnt = 0;
98-
for (char c : S.toCharArray()) {
119+
for (int i = 0; i < s.length(); ++i) {
120+
char c = s.charAt(i);
99121
if (c == '(') {
100122
if (++cnt > 1) {
101-
res.append('(');
123+
ans.append(c);
102124
}
103125
} else {
104126
if (--cnt > 0) {
105-
res.append(')');
127+
ans.append(c);
106128
}
107129
}
108130
}
109-
return res.toString();
131+
return ans.toString();
132+
}
133+
}
134+
```
135+
136+
```java
137+
class Solution {
138+
public String removeOuterParentheses(String s) {
139+
StringBuilder ans = new StringBuilder();
140+
int cnt = 0;
141+
for (int i = 0; i < s.length(); ++i) {
142+
char c = s.charAt(i);
143+
if (c == '(') {
144+
++cnt;
145+
}
146+
if (cnt > 1) {
147+
ans.append(c);
148+
}
149+
if (c == ')') {
150+
--cnt;
151+
}
152+
}
153+
return ans.toString();
110154
}
111155
}
112156
```
@@ -117,20 +161,42 @@ class Solution {
117161
class Solution {
118162
public:
119163
string removeOuterParentheses(string s) {
120-
string res;
121-
int depth = 0;
122-
for (char c : s) {
164+
string ans;
165+
int cnt = 0;
166+
for (char& c : s) {
123167
if (c == '(') {
124-
depth++;
168+
if (++cnt > 1) {
169+
ans.push_back(c);
170+
}
171+
} else {
172+
if (--cnt) {
173+
ans.push_back(c);
174+
}
125175
}
126-
if (depth != 1) {
127-
res.push_back(c);
176+
}
177+
return ans;
178+
}
179+
};
180+
```
181+
182+
```cpp
183+
class Solution {
184+
public:
185+
string removeOuterParentheses(string s) {
186+
string ans;
187+
int cnt = 0;
188+
for (char& c : s) {
189+
if (c == '(') {
190+
++cnt;
191+
}
192+
if (cnt > 1) {
193+
ans.push_back(c);
128194
}
129195
if (c == ')') {
130-
depth--;
196+
--cnt;
131197
}
132198
}
133-
return res;
199+
return ans;
134200
}
135201
};
136202
```
@@ -158,6 +224,25 @@ func removeOuterParentheses(s string) string {
158224
}
159225
```
160226

227+
```go
228+
func removeOuterParentheses(s string) string {
229+
ans := []rune{}
230+
cnt := 0
231+
for _, c := range s {
232+
if c == '(' {
233+
cnt++
234+
}
235+
if cnt > 1 {
236+
ans = append(ans, c)
237+
}
238+
if c == ')' {
239+
cnt--
240+
}
241+
}
242+
return string(ans)
243+
}
244+
```
245+
161246
### **TypeScript**
162247

163248
```ts

solution/1000-1099/1021.Remove Outermost Parentheses/README_EN.md

Lines changed: 93 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,63 @@ class Solution:
7979
return ''.join(ans)
8080
```
8181

82+
```python
83+
class Solution:
84+
def removeOuterParentheses(self, s: str) -> str:
85+
ans = []
86+
cnt = 0
87+
for c in s:
88+
if c == '(':
89+
cnt += 1
90+
if cnt > 1:
91+
ans.append(c)
92+
if c == ')':
93+
cnt -= 1
94+
return ''.join(ans)
95+
```
96+
8297
### **Java**
8398

8499
```java
85100
class Solution {
86-
public String removeOuterParentheses(String S) {
87-
StringBuilder res = new StringBuilder();
101+
public String removeOuterParentheses(String s) {
102+
StringBuilder ans = new StringBuilder();
88103
int cnt = 0;
89-
for (char c : S.toCharArray()) {
104+
for (int i = 0; i < s.length(); ++i) {
105+
char c = s.charAt(i);
90106
if (c == '(') {
91107
if (++cnt > 1) {
92-
res.append('(');
108+
ans.append(c);
93109
}
94110
} else {
95111
if (--cnt > 0) {
96-
res.append(')');
112+
ans.append(c);
97113
}
98114
}
99115
}
100-
return res.toString();
116+
return ans.toString();
117+
}
118+
}
119+
```
120+
121+
```java
122+
class Solution {
123+
public String removeOuterParentheses(String s) {
124+
StringBuilder ans = new StringBuilder();
125+
int cnt = 0;
126+
for (int i = 0; i < s.length(); ++i) {
127+
char c = s.charAt(i);
128+
if (c == '(') {
129+
++cnt;
130+
}
131+
if (cnt > 1) {
132+
ans.append(c);
133+
}
134+
if (c == ')') {
135+
--cnt;
136+
}
137+
}
138+
return ans.toString();
101139
}
102140
}
103141
```
@@ -108,20 +146,42 @@ class Solution {
108146
class Solution {
109147
public:
110148
string removeOuterParentheses(string s) {
111-
string res;
112-
int depth = 0;
113-
for (char c : s) {
149+
string ans;
150+
int cnt = 0;
151+
for (char& c : s) {
152+
if (c == '(') {
153+
if (++cnt > 1) {
154+
ans.push_back(c);
155+
}
156+
} else {
157+
if (--cnt) {
158+
ans.push_back(c);
159+
}
160+
}
161+
}
162+
return ans;
163+
}
164+
};
165+
```
166+
167+
```cpp
168+
class Solution {
169+
public:
170+
string removeOuterParentheses(string s) {
171+
string ans;
172+
int cnt = 0;
173+
for (char& c : s) {
114174
if (c == '(') {
115-
depth++;
175+
++cnt;
116176
}
117-
if (depth != 1) {
118-
res.push_back(c);
177+
if (cnt > 1) {
178+
ans.push_back(c);
119179
}
120180
if (c == ')') {
121-
depth--;
181+
--cnt;
122182
}
123183
}
124-
return res;
184+
return ans;
125185
}
126186
};
127187
```
@@ -149,6 +209,25 @@ func removeOuterParentheses(s string) string {
149209
}
150210
```
151211

212+
```go
213+
func removeOuterParentheses(s string) string {
214+
ans := []rune{}
215+
cnt := 0
216+
for _, c := range s {
217+
if c == '(' {
218+
cnt++
219+
}
220+
if cnt > 1 {
221+
ans = append(ans, c)
222+
}
223+
if c == ')' {
224+
cnt--
225+
}
226+
}
227+
return string(ans)
228+
}
229+
```
230+
152231
### **TypeScript**
153232

154233
```ts
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
class Solution {
22
public:
33
string removeOuterParentheses(string s) {
4-
string res;
5-
int depth = 0;
6-
for (char c : s) {
4+
string ans;
5+
int cnt = 0;
6+
for (char& c : s) {
77
if (c == '(') {
8-
depth++;
9-
}
10-
if (depth != 1) {
11-
res.push_back(c);
12-
}
13-
if (c == ')') {
14-
depth--;
8+
if (++cnt > 1) {
9+
ans.push_back(c);
10+
}
11+
} else {
12+
if (--cnt) {
13+
ans.push_back(c);
14+
}
1515
}
1616
}
17-
return res;
17+
return ans;
1818
}
1919
};
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
class Solution {
2-
public String removeOuterParentheses(String S) {
3-
StringBuilder res = new StringBuilder();
2+
public String removeOuterParentheses(String s) {
3+
StringBuilder ans = new StringBuilder();
44
int cnt = 0;
5-
for (char c : S.toCharArray()) {
5+
for (int i = 0; i < s.length(); ++i) {
6+
char c = s.charAt(i);
67
if (c == '(') {
78
if (++cnt > 1) {
8-
res.append('(');
9+
ans.append(c);
910
}
1011
} else {
1112
if (--cnt > 0) {
12-
res.append(')');
13+
ans.append(c);
1314
}
1415
}
1516
}
16-
return res.toString();
17+
return ans.toString();
1718
}
18-
}
19+
}

solution/1000-1099/1022.Sum of Root To Leaf Binary Numbers/README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,16 @@
4646

4747
<!-- 这里可写通用的实现逻辑 -->
4848

49-
深度优先搜索 DFS 实现。
49+
**方法一:递归**
50+
51+
我们设计递归函数 `dfs(root, t)`,它接收两个参数:当前节点 `root` 和当前节点的父节点对应的二进制数 `t`。函数的返回值是从当前节点到叶子节点的路径所表示的二进制数之和。答案即为 `dfs(root, 0)`
52+
53+
递归函数的逻辑如下:
54+
55+
- 如果当前节点 `root` 为空,则返回 `0`,否则计算当前节点对应的二进制数 `t`,即 `t = t << 1 | root.val`
56+
- 如果当前节点是叶子节点,则返回 `t`,否则返回 `dfs(root.left, t)``dfs(root.right, t)` 的和。
57+
58+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点数。对每个节点访问一次;递归栈需要 $O(n)$ 的空间。
5059

5160
<!-- tabs:start -->
5261

0 commit comments

Comments
 (0)