File tree Expand file tree Collapse file tree 5 files changed +221
-47
lines changed
1021.Remove Outermost Parentheses
1022.Sum of Root To Leaf Binary Numbers Expand file tree Collapse file tree 5 files changed +221
-47
lines changed Original file line number Diff line number Diff line change 63
63
64
64
<!-- 这里可写通用的实现逻辑 -->
65
65
66
+ ** 方法一:计数**
67
+
68
+ 遍历字符串,遇到左括号 ` ( ` 计数器加一,此时计数器不为 1 时,说明当前括号不是最外层括号,将其加入结果字符串。遇到右括号 ` ) ` 计数器减一,此时计数器不为 0 时,说明当前括号不是最外层括号,将其加入结果字符串。
69
+
70
+ 时间复杂度 $O(n)$,忽略答案字符串的空间开销,空间复杂度 $O(1)$。其中 $n$ 为字符串长度。
71
+
66
72
<!-- tabs:start -->
67
73
68
74
### ** Python3**
@@ -86,27 +92,65 @@ class Solution:
86
92
return ' ' .join(ans)
87
93
```
88
94
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
+
89
110
### ** Java**
90
111
91
112
<!-- 这里可写当前语言的特殊实现逻辑 -->
92
113
93
114
``` java
94
115
class Solution {
95
- public String removeOuterParentheses (String S ) {
96
- StringBuilder res = new StringBuilder ();
116
+ public String removeOuterParentheses (String s ) {
117
+ StringBuilder ans = new StringBuilder ();
97
118
int cnt = 0 ;
98
- for (char c : S . toCharArray()) {
119
+ for (int i = 0 ; i < s. length(); ++ i) {
120
+ char c = s. charAt(i);
99
121
if (c == ' (' ) {
100
122
if (++ cnt > 1 ) {
101
- res . append(' ( ' );
123
+ ans . append(c );
102
124
}
103
125
} else {
104
126
if (-- cnt > 0 ) {
105
- res . append(' ) ' );
127
+ ans . append(c );
106
128
}
107
129
}
108
130
}
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();
110
154
}
111
155
}
112
156
```
@@ -117,20 +161,42 @@ class Solution {
117
161
class Solution {
118
162
public:
119
163
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) {
123
167
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
+ }
125
175
}
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);
128
194
}
129
195
if (c == ')') {
130
- depth-- ;
196
+ --cnt ;
131
197
}
132
198
}
133
- return res ;
199
+ return ans ;
134
200
}
135
201
};
136
202
```
@@ -158,6 +224,25 @@ func removeOuterParentheses(s string) string {
158
224
}
159
225
```
160
226
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
+
161
246
### ** TypeScript**
162
247
163
248
``` ts
Original file line number Diff line number Diff line change @@ -79,25 +79,63 @@ class Solution:
79
79
return ' ' .join(ans)
80
80
```
81
81
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
+
82
97
### ** Java**
83
98
84
99
``` java
85
100
class Solution {
86
- public String removeOuterParentheses (String S ) {
87
- StringBuilder res = new StringBuilder ();
101
+ public String removeOuterParentheses (String s ) {
102
+ StringBuilder ans = new StringBuilder ();
88
103
int cnt = 0 ;
89
- for (char c : S . toCharArray()) {
104
+ for (int i = 0 ; i < s. length(); ++ i) {
105
+ char c = s. charAt(i);
90
106
if (c == ' (' ) {
91
107
if (++ cnt > 1 ) {
92
- res . append(' ( ' );
108
+ ans . append(c );
93
109
}
94
110
} else {
95
111
if (-- cnt > 0 ) {
96
- res . append(' ) ' );
112
+ ans . append(c );
97
113
}
98
114
}
99
115
}
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();
101
139
}
102
140
}
103
141
```
@@ -108,20 +146,42 @@ class Solution {
108
146
class Solution {
109
147
public:
110
148
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) {
114
174
if (c == '(') {
115
- depth++ ;
175
+ ++cnt ;
116
176
}
117
- if (depth != 1) {
118
- res .push_back(c);
177
+ if (cnt > 1) {
178
+ ans .push_back(c);
119
179
}
120
180
if (c == ')') {
121
- depth-- ;
181
+ --cnt ;
122
182
}
123
183
}
124
- return res ;
184
+ return ans ;
125
185
}
126
186
};
127
187
```
@@ -149,6 +209,25 @@ func removeOuterParentheses(s string) string {
149
209
}
150
210
```
151
211
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
+
152
231
### ** TypeScript**
153
232
154
233
``` ts
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public:
3
3
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) {
7
7
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
+ }
15
15
}
16
16
}
17
- return res ;
17
+ return ans ;
18
18
}
19
19
};
Original file line number Diff line number Diff line change 1
1
class Solution {
2
- public String removeOuterParentheses (String S ) {
3
- StringBuilder res = new StringBuilder ();
2
+ public String removeOuterParentheses (String s ) {
3
+ StringBuilder ans = new StringBuilder ();
4
4
int cnt = 0 ;
5
- for (char c : S .toCharArray ()) {
5
+ for (int i = 0 ; i < s .length (); ++i ) {
6
+ char c = s .charAt (i );
6
7
if (c == '(' ) {
7
8
if (++cnt > 1 ) {
8
- res .append ('(' );
9
+ ans .append (c );
9
10
}
10
11
} else {
11
12
if (--cnt > 0 ) {
12
- res .append (')' );
13
+ ans .append (c );
13
14
}
14
15
}
15
16
}
16
- return res .toString ();
17
+ return ans .toString ();
17
18
}
18
- }
19
+ }
Original file line number Diff line number Diff line change 46
46
47
47
<!-- 这里可写通用的实现逻辑 -->
48
48
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)$ 的空间。
50
59
51
60
<!-- tabs:start -->
52
61
You can’t perform that action at this time.
0 commit comments