66
66
67
67
### Solution 1: Simulation
68
68
69
- We can use a double-ended queue or stack to simulate the reversal process.
69
+ We can directly use a stack to simulate the reversal process.
70
70
71
- The time complexity is $O(n^2)$, where $n$ is the length of the string $s$.
71
+ The time complexity is $O(n^2)$, and the space complexity is $O(n)$, where $n$ is the length of the string $s$.
72
72
73
73
<!-- tabs:start -->
74
74
@@ -79,46 +79,37 @@ class Solution:
79
79
def reverseParentheses (self , s : str ) -> str :
80
80
stk = []
81
81
for c in s:
82
- if c == ' ) ' :
82
+ if c == " ) " :
83
83
t = []
84
- while stk[- 1 ] != ' ( ' :
84
+ while stk[- 1 ] != " ( " :
85
85
t.append(stk.pop())
86
86
stk.pop()
87
87
stk.extend(t)
88
88
else :
89
89
stk.append(c)
90
- return ' ' .join(stk)
90
+ return " " .join(stk)
91
91
```
92
92
93
93
#### Java
94
94
95
95
``` java
96
96
class Solution {
97
97
public String reverseParentheses (String s ) {
98
- int n = s. length();
99
- int [] d = new int [n];
100
- Deque<Integer > stk = new ArrayDeque<> ();
101
- for (int i = 0 ; i < n; ++ i) {
102
- if (s. charAt(i) == ' (' ) {
103
- stk. push(i);
104
- } else if (s. charAt(i) == ' )' ) {
105
- int j = stk. pop();
106
- d[i] = j;
107
- d[j] = i;
108
- }
109
- }
110
- StringBuilder ans = new StringBuilder ();
111
- int i = 0 , x = 1 ;
112
- while (i < n) {
113
- if (s. charAt(i) == ' (' || s. charAt(i) == ' )' ) {
114
- i = d[i];
115
- x = - x;
98
+ StringBuilder stk = new StringBuilder ();
99
+ for (char c : s. toCharArray()) {
100
+ if (c == ' )' ) {
101
+ StringBuilder t = new StringBuilder ();
102
+ while (stk. charAt(stk. length() - 1 ) != ' (' ) {
103
+ t. append(stk. charAt(stk. length() - 1 ));
104
+ stk. deleteCharAt(stk. length() - 1 );
105
+ }
106
+ stk. deleteCharAt(stk. length() - 1 );
107
+ stk. append(t);
116
108
} else {
117
- ans . append(s . charAt(i) );
109
+ stk . append(c );
118
110
}
119
- i += x;
120
111
}
121
- return ans . toString();
112
+ return stk . toString();
122
113
}
123
114
}
124
115
```
@@ -170,6 +161,27 @@ func reverseParentheses(s string) string {
170
161
}
171
162
```
172
163
164
+ #### TypeScript
165
+
166
+ ``` ts
167
+ function reverseParentheses(s : string ): string {
168
+ const stk: string [] = [];
169
+ for (const c of s ) {
170
+ if (c === ' )' ) {
171
+ const t: string [] = [];
172
+ while (stk .at (- 1 )! !== ' (' ) {
173
+ t .push (stk .pop ()! );
174
+ }
175
+ stk .pop ();
176
+ stk .push (... t );
177
+ } else {
178
+ stk .push (c );
179
+ }
180
+ }
181
+ return stk .join (' ' );
182
+ }
183
+ ```
184
+
173
185
#### JavaScript
174
186
175
187
``` js
@@ -178,83 +190,38 @@ func reverseParentheses(s string) string {
178
190
* @return {string}
179
191
*/
180
192
var reverseParentheses = function (s ) {
181
- const n = s .length ;
182
- const d = new Array (n).fill (0 );
183
193
const stk = [];
184
- for (let i = 0 ; i < n; ++ i) {
185
- if (s[i] == ' (' ) {
186
- stk .push (i);
187
- } else if (s[i] == ' )' ) {
188
- const j = stk .pop ();
189
- d[i] = j;
190
- d[j] = i;
191
- }
192
- }
193
- let i = 0 ;
194
- let x = 1 ;
195
- const ans = [];
196
- while (i < n) {
197
- const c = s .charAt (i);
198
- if (c == ' (' || c == ' )' ) {
199
- i = d[i];
200
- x = - x;
194
+ for (const c of s) {
195
+ if (c === ' )' ) {
196
+ const t = [];
197
+ while (stk .at (- 1 ) !== ' (' ) {
198
+ t .push (stk .pop ());
199
+ }
200
+ stk .pop ();
201
+ stk .push (... t);
201
202
} else {
202
- ans .push (c);
203
+ stk .push (c);
203
204
}
204
- i += x;
205
205
}
206
- return ans .join (' ' );
206
+ return stk .join (' ' );
207
207
};
208
208
```
209
209
210
- #### TypeScript
211
-
212
- ``` ts
213
- function reverseParentheses(s : string ): string {
214
- const n = s .length ;
215
- const d = new Array (n ).fill (0 );
216
- const stk: number [] = [];
217
- for (let i = 0 ; i < n ; ++ i ) {
218
- if (s [i ] === ' (' ) {
219
- stk .push (i );
220
- } else if (s [i ] === ' )' ) {
221
- const j = stk .pop ()! ;
222
- d [i ] = j ;
223
- d [j ] = i ;
224
- }
225
- }
226
- let i = 0 ;
227
- let x = 1 ;
228
- const ans: string [] = [];
229
- while (i < n ) {
230
- const c = s .charAt (i );
231
- if (c === ' (' || c === ' )' ) {
232
- i = d [i ];
233
- x = - x ;
234
- } else {
235
- ans .push (c );
236
- }
237
- i += x ;
238
- }
239
- return ans .join (' ' );
240
- }
241
- ```
242
-
243
210
<!-- tabs: end -->
244
211
245
212
<!-- solution: end -->
246
213
247
214
<!-- solution: start -->
248
215
249
- ### Solution 2: Quick Thinking
216
+ ### Solution 2: Brain Teaser
250
217
251
- We observe that during the traversal of the string, each time we encounter '(' or ')' , we jump to the corresponding ')' or '(', then reverse the traversal direction and continue.
218
+ We observe that, when traversing the string, each time we encounter ` ( ` or ` ) ` , we jump to the corresponding ` ) ` or ` ( ` and then reverse the direction of traversal to continue.
252
219
253
- Therefore, we can use an array $d$ to record the position of the other bracket corresponding to each '(' or ')' , i.e., $d[ i] $ represents the position of the other bracket corresponding to the bracket at position $i$. We can directly use a stack to calculate the array $d$.
220
+ Therefore, we can use an array $d$ to record the position of the corresponding other bracket for each ` ( ` or ` ) ` , i.e., $d[ i] $ represents the position of the other bracket corresponding to the bracket at position $i$. We can directly use a stack to compute the array $d$.
254
221
255
- Then, we traverse the string from left to right. When we encounter '(' or ')' , we jump to the corresponding position according to the array $d$, then reverse the direction and continue to traverse until the entire string is traversed.
222
+ Then, we traverse the string from left to right. When encountering ` ( ` or ` ) ` , we jump to the corresponding position according to the array $d$, then reverse the direction and continue traversing until the entire string is traversed.
256
223
257
- The time complexity is $O(n)$, where $n$ is the length of the string $s$.
224
+ The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the string $s$.
258
225
259
226
<!-- tabs: start -->
260
227
@@ -267,21 +234,54 @@ class Solution:
267
234
d = [0 ] * n
268
235
stk = []
269
236
for i, c in enumerate (s):
270
- if c == ' ( ' :
237
+ if c == " ( " :
271
238
stk.append(i)
272
- elif c == ' ) ' :
239
+ elif c == " ) " :
273
240
j = stk.pop()
274
241
d[i], d[j] = j, i
275
242
i, x = 0 , 1
276
243
ans = []
277
244
while i < n:
278
- if s[i] in ' () ' :
245
+ if s[i] in " () " :
279
246
i = d[i]
280
247
x = - x
281
248
else :
282
249
ans.append(s[i])
283
250
i += x
284
- return ' ' .join(ans)
251
+ return " " .join(ans)
252
+ ```
253
+
254
+ #### Java
255
+
256
+ ``` java
257
+ class Solution {
258
+ public String reverseParentheses (String s ) {
259
+ int n = s. length();
260
+ int [] d = new int [n];
261
+ Deque<Integer > stk = new ArrayDeque<> ();
262
+ for (int i = 0 ; i < n; ++ i) {
263
+ if (s. charAt(i) == ' (' ) {
264
+ stk. push(i);
265
+ } else if (s. charAt(i) == ' )' ) {
266
+ int j = stk. pop();
267
+ d[i] = j;
268
+ d[j] = i;
269
+ }
270
+ }
271
+ StringBuilder ans = new StringBuilder ();
272
+ int i = 0 , x = 1 ;
273
+ while (i < n) {
274
+ if (s. charAt(i) == ' (' || s. charAt(i) == ' )' ) {
275
+ i = d[i];
276
+ x = - x;
277
+ } else {
278
+ ans. append(s. charAt(i));
279
+ }
280
+ i += x;
281
+ }
282
+ return ans. toString();
283
+ }
284
+ }
285
285
```
286
286
287
287
#### C++
@@ -350,80 +350,74 @@ func reverseParentheses(s string) string {
350
350
}
351
351
```
352
352
353
- #### JavaScript
353
+ #### TypeScript
354
354
355
- ``` js
356
- function reverseParentheses (s ) {
357
- const res = [];
355
+ ``` ts
356
+ function reverseParentheses(s : string ): string {
358
357
const n = s .length ;
359
- const d = Array (n).fill (- 1 );
360
- const stk = [];
361
-
362
- for ( let i = 0 ; i < n; i ++ ) {
363
- if (s[i] === ' ( ' ) stk .push (i);
364
- else if (s[i] === ' )' ) {
365
- const j = stk .pop ();
358
+ const d: number [] = Array (n ).fill (0 );
359
+ const stk: number [] = [];
360
+ for ( let i = 0 ; i < n ; ++ i ) {
361
+ if ( s [ i ] === ' ( ' ) {
362
+ stk .push (i );
363
+ } else if (s [i ] === ' )' ) {
364
+ const j = stk .pop ()! ;
366
365
d [i ] = j ;
367
366
d [j ] = i ;
368
367
}
369
368
}
370
-
371
- for (let i = 0 , forward = true ; i < n; ) {
372
- const ch = s[i];
373
-
374
- switch (s[i]) {
375
- case ' (' :
376
- case ' )' :
377
- i = forward ? d[i] - 1 : d[i] + 1 ;
378
- forward = ! forward;
379
- break ;
380
-
381
- default :
382
- res .push (ch);
383
- i = forward ? i + 1 : i - 1 ;
369
+ let i = 0 ;
370
+ let x = 1 ;
371
+ const ans: string [] = [];
372
+ while (i < n ) {
373
+ const c = s .charAt (i );
374
+ if (' ()' .includes (c )) {
375
+ i = d [i ];
376
+ x = - x ;
377
+ } else {
378
+ ans .push (c );
384
379
}
380
+ i += x ;
385
381
}
386
-
387
- return res .join (' ' );
382
+ return ans .join (' ' );
388
383
}
389
384
```
390
385
391
- #### TypeScript
386
+ #### JavaScript
392
387
393
- ``` ts
394
- function reverseParentheses(s : string ): string {
395
- const res: string [] = [];
388
+ ``` js
389
+ /**
390
+ * @param {string} s
391
+ * @return {string}
392
+ */
393
+ var reverseParentheses = function (s ) {
396
394
const n = s .length ;
397
- const d = Array (n ).fill (- 1 );
398
- const stk: number [] = [];
399
-
400
- for ( let i = 0 ; i < n ; i ++ ) {
401
- if ( s [ i ] === ' ( ' ) stk .push (i );
402
- else if (s [i ] === ' )' ) {
403
- const j = stk .pop ()! ;
395
+ const d = Array (n).fill (0 );
396
+ const stk = [];
397
+ for ( let i = 0 ; i < n; ++ i) {
398
+ if (s[i] === ' ( ' ) {
399
+ stk .push (i);
400
+ } else if (s[i] === ' )' ) {
401
+ const j = stk .pop ();
404
402
d[i] = j;
405
403
d[j] = i;
406
404
}
407
405
}
408
-
409
- for (let i = 0 , forward = true ; i < n ; ) {
410
- const ch = s [i ];
411
-
412
- switch (s [i ]) {
413
- case ' (' :
414
- case ' )' :
415
- i = forward ? d [i ] - 1 : d [i ] + 1 ;
416
- forward = ! forward ;
417
- break ;
418
-
419
- default :
420
- res .push (ch );
421
- i = forward ? i + 1 : i - 1 ;
406
+ let i = 0 ;
407
+ let x = 1 ;
408
+ const ans = [];
409
+ while (i < n) {
410
+ const c = s .charAt (i);
411
+ if (' ()' .includes (c)) {
412
+ i = d[i];
413
+ x = - x;
414
+ } else {
415
+ ans .push (c);
422
416
}
417
+ i += x;
423
418
}
424
-
425
- return res .join (' ' );
426
- }
419
+ return ans .join (' ' );
420
+ };
427
421
```
428
422
429
423
<!-- tabs: end -->
0 commit comments