73
73
74
74
### 方法一:模拟
75
75
76
- 用双端队列或者栈,模拟反转的过程 。
76
+ 我们可以直接用栈来模拟反转的过程 。
77
77
78
- 时间复杂度 $O(n^2)$,其中 $n$ 为字符串 $s$ 的长度。
78
+ 时间复杂度 $O(n^2)$,空间复杂度 $O(n)$, 其中 $n$ 为字符串 $s$ 的长度。
79
79
80
80
<!-- tabs:start -->
81
81
@@ -86,46 +86,37 @@ class Solution:
86
86
def reverseParentheses (self , s : str ) -> str :
87
87
stk = []
88
88
for c in s:
89
- if c == ' ) ' :
89
+ if c == " ) " :
90
90
t = []
91
- while stk[- 1 ] != ' ( ' :
91
+ while stk[- 1 ] != " ( " :
92
92
t.append(stk.pop())
93
93
stk.pop()
94
94
stk.extend(t)
95
95
else :
96
96
stk.append(c)
97
- return ' ' .join(stk)
97
+ return " " .join(stk)
98
98
```
99
99
100
100
#### Java
101
101
102
102
``` java
103
103
class Solution {
104
104
public String reverseParentheses (String s ) {
105
- int n = s. length();
106
- int [] d = new int [n];
107
- Deque<Integer > stk = new ArrayDeque<> ();
108
- for (int i = 0 ; i < n; ++ i) {
109
- if (s. charAt(i) == ' (' ) {
110
- stk. push(i);
111
- } else if (s. charAt(i) == ' )' ) {
112
- int j = stk. pop();
113
- d[i] = j;
114
- d[j] = i;
115
- }
116
- }
117
- StringBuilder ans = new StringBuilder ();
118
- int i = 0 , x = 1 ;
119
- while (i < n) {
120
- if (s. charAt(i) == ' (' || s. charAt(i) == ' )' ) {
121
- i = d[i];
122
- x = - x;
105
+ StringBuilder stk = new StringBuilder ();
106
+ for (char c : s. toCharArray()) {
107
+ if (c == ' )' ) {
108
+ StringBuilder t = new StringBuilder ();
109
+ while (stk. charAt(stk. length() - 1 ) != ' (' ) {
110
+ t. append(stk. charAt(stk. length() - 1 ));
111
+ stk. deleteCharAt(stk. length() - 1 );
112
+ }
113
+ stk. deleteCharAt(stk. length() - 1 );
114
+ stk. append(t);
123
115
} else {
124
- ans . append(s . charAt(i) );
116
+ stk . append(c );
125
117
}
126
- i += x;
127
118
}
128
- return ans . toString();
119
+ return stk . toString();
129
120
}
130
121
}
131
122
```
@@ -177,6 +168,27 @@ func reverseParentheses(s string) string {
177
168
}
178
169
```
179
170
171
+ #### TypeScript
172
+
173
+ ``` ts
174
+ function reverseParentheses(s : string ): string {
175
+ const stk: string [] = [];
176
+ for (const c of s ) {
177
+ if (c === ' )' ) {
178
+ const t: string [] = [];
179
+ while (stk .at (- 1 )! !== ' (' ) {
180
+ t .push (stk .pop ()! );
181
+ }
182
+ stk .pop ();
183
+ stk .push (... t );
184
+ } else {
185
+ stk .push (c );
186
+ }
187
+ }
188
+ return stk .join (' ' );
189
+ }
190
+ ```
191
+
180
192
#### JavaScript
181
193
182
194
``` js
@@ -185,68 +197,23 @@ func reverseParentheses(s string) string {
185
197
* @return {string}
186
198
*/
187
199
var reverseParentheses = function (s ) {
188
- const n = s .length ;
189
- const d = new Array (n).fill (0 );
190
200
const stk = [];
191
- for (let i = 0 ; i < n; ++ i) {
192
- if (s[i] == ' (' ) {
193
- stk .push (i);
194
- } else if (s[i] == ' )' ) {
195
- const j = stk .pop ();
196
- d[i] = j;
197
- d[j] = i;
198
- }
199
- }
200
- let i = 0 ;
201
- let x = 1 ;
202
- const ans = [];
203
- while (i < n) {
204
- const c = s .charAt (i);
205
- if (c == ' (' || c == ' )' ) {
206
- i = d[i];
207
- x = - x;
201
+ for (const c of s) {
202
+ if (c === ' )' ) {
203
+ const t = [];
204
+ while (stk .at (- 1 ) !== ' (' ) {
205
+ t .push (stk .pop ());
206
+ }
207
+ stk .pop ();
208
+ stk .push (... t);
208
209
} else {
209
- ans .push (c);
210
+ stk .push (c);
210
211
}
211
- i += x;
212
212
}
213
- return ans .join (' ' );
213
+ return stk .join (' ' );
214
214
};
215
215
```
216
216
217
- #### TypeScript
218
-
219
- ``` ts
220
- function reverseParentheses(s : string ): string {
221
- const n = s .length ;
222
- const d = new Array (n ).fill (0 );
223
- const stk: number [] = [];
224
- for (let i = 0 ; i < n ; ++ i ) {
225
- if (s [i ] === ' (' ) {
226
- stk .push (i );
227
- } else if (s [i ] === ' )' ) {
228
- const j = stk .pop ()! ;
229
- d [i ] = j ;
230
- d [j ] = i ;
231
- }
232
- }
233
- let i = 0 ;
234
- let x = 1 ;
235
- const ans: string [] = [];
236
- while (i < n ) {
237
- const c = s .charAt (i );
238
- if (c === ' (' || c === ' )' ) {
239
- i = d [i ];
240
- x = - x ;
241
- } else {
242
- ans .push (c );
243
- }
244
- i += x ;
245
- }
246
- return ans .join (' ' );
247
- }
248
- ```
249
-
250
217
<!-- tabs: end -->
251
218
252
219
<!-- solution: end -->
@@ -261,7 +228,7 @@ function reverseParentheses(s: string): string {
261
228
262
229
然后,我们从左到右遍历字符串,遇到 ` ( ` 或者 ` ) ` 时,根据 $d$ 数组跳到对应的位置,然后反转方向,继续遍历,直到遍历完整个字符串。
263
230
264
- 时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。
231
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$, 其中 $n$ 为字符串 $s$ 的长度。
265
232
266
233
<!-- tabs: start -->
267
234
@@ -274,21 +241,54 @@ class Solution:
274
241
d = [0 ] * n
275
242
stk = []
276
243
for i, c in enumerate (s):
277
- if c == ' ( ' :
244
+ if c == " ( " :
278
245
stk.append(i)
279
- elif c == ' ) ' :
246
+ elif c == " ) " :
280
247
j = stk.pop()
281
248
d[i], d[j] = j, i
282
249
i, x = 0 , 1
283
250
ans = []
284
251
while i < n:
285
- if s[i] in ' () ' :
252
+ if s[i] in " () " :
286
253
i = d[i]
287
254
x = - x
288
255
else :
289
256
ans.append(s[i])
290
257
i += x
291
- return ' ' .join(ans)
258
+ return " " .join(ans)
259
+ ```
260
+
261
+ #### Java
262
+
263
+ ``` java
264
+ class Solution {
265
+ public String reverseParentheses (String s ) {
266
+ int n = s. length();
267
+ int [] d = new int [n];
268
+ Deque<Integer > stk = new ArrayDeque<> ();
269
+ for (int i = 0 ; i < n; ++ i) {
270
+ if (s. charAt(i) == ' (' ) {
271
+ stk. push(i);
272
+ } else if (s. charAt(i) == ' )' ) {
273
+ int j = stk. pop();
274
+ d[i] = j;
275
+ d[j] = i;
276
+ }
277
+ }
278
+ StringBuilder ans = new StringBuilder ();
279
+ int i = 0 , x = 1 ;
280
+ while (i < n) {
281
+ if (s. charAt(i) == ' (' || s. charAt(i) == ' )' ) {
282
+ i = d[i];
283
+ x = - x;
284
+ } else {
285
+ ans. append(s. charAt(i));
286
+ }
287
+ i += x;
288
+ }
289
+ return ans. toString();
290
+ }
291
+ }
292
292
```
293
293
294
294
#### C++
@@ -357,80 +357,74 @@ func reverseParentheses(s string) string {
357
357
}
358
358
```
359
359
360
- #### JavaScript
360
+ #### TypeScript
361
361
362
- ``` js
363
- function reverseParentheses (s ) {
364
- const res = [];
362
+ ``` ts
363
+ function reverseParentheses(s : string ): string {
365
364
const n = s .length ;
366
- const d = Array (n).fill (- 1 );
367
- const stk = [];
368
-
369
- for ( let i = 0 ; i < n; i ++ ) {
370
- if (s[i] === ' ( ' ) stk .push (i);
371
- else if (s[i] === ' )' ) {
372
- const j = stk .pop ();
365
+ const d: number [] = Array (n ).fill (0 );
366
+ const stk: number [] = [];
367
+ for ( let i = 0 ; i < n ; ++ i ) {
368
+ if ( s [ i ] === ' ( ' ) {
369
+ stk .push (i );
370
+ } else if (s [i ] === ' )' ) {
371
+ const j = stk .pop ()! ;
373
372
d [i ] = j ;
374
373
d [j ] = i ;
375
374
}
376
375
}
377
-
378
- for (let i = 0 , forward = true ; i < n; ) {
379
- const ch = s[i];
380
-
381
- switch (s[i]) {
382
- case ' (' :
383
- case ' )' :
384
- i = forward ? d[i] - 1 : d[i] + 1 ;
385
- forward = ! forward;
386
- break ;
387
-
388
- default :
389
- res .push (ch);
390
- i = forward ? i + 1 : i - 1 ;
376
+ let i = 0 ;
377
+ let x = 1 ;
378
+ const ans: string [] = [];
379
+ while (i < n ) {
380
+ const c = s .charAt (i );
381
+ if (' ()' .includes (c )) {
382
+ i = d [i ];
383
+ x = - x ;
384
+ } else {
385
+ ans .push (c );
391
386
}
387
+ i += x ;
392
388
}
393
-
394
- return res .join (' ' );
389
+ return ans .join (' ' );
395
390
}
396
391
```
397
392
398
- #### TypeScript
393
+ #### JavaScript
399
394
400
- ``` ts
401
- function reverseParentheses(s : string ): string {
402
- const res: string [] = [];
395
+ ``` js
396
+ /**
397
+ * @param {string} s
398
+ * @return {string}
399
+ */
400
+ var reverseParentheses = function (s ) {
403
401
const n = s .length ;
404
- const d = Array (n ).fill (- 1 );
405
- const stk: number [] = [];
406
-
407
- for ( let i = 0 ; i < n ; i ++ ) {
408
- if ( s [ i ] === ' ( ' ) stk .push (i );
409
- else if (s [i ] === ' )' ) {
410
- const j = stk .pop ()! ;
402
+ const d = Array (n).fill (0 );
403
+ const stk = [];
404
+ for ( let i = 0 ; i < n; ++ i) {
405
+ if (s[i] === ' ( ' ) {
406
+ stk .push (i);
407
+ } else if (s[i] === ' )' ) {
408
+ const j = stk .pop ();
411
409
d[i] = j;
412
410
d[j] = i;
413
411
}
414
412
}
415
-
416
- for (let i = 0 , forward = true ; i < n ; ) {
417
- const ch = s [i ];
418
-
419
- switch (s [i ]) {
420
- case ' (' :
421
- case ' )' :
422
- i = forward ? d [i ] - 1 : d [i ] + 1 ;
423
- forward = ! forward ;
424
- break ;
425
-
426
- default :
427
- res .push (ch );
428
- i = forward ? i + 1 : i - 1 ;
413
+ let i = 0 ;
414
+ let x = 1 ;
415
+ const ans = [];
416
+ while (i < n) {
417
+ const c = s .charAt (i);
418
+ if (' ()' .includes (c)) {
419
+ i = d[i];
420
+ x = - x;
421
+ } else {
422
+ ans .push (c);
429
423
}
424
+ i += x;
430
425
}
431
-
432
- return res .join (' ' );
433
- }
426
+ return ans .join (' ' );
427
+ };
434
428
```
435
429
436
430
<!-- tabs: end -->
0 commit comments