File tree Expand file tree Collapse file tree 1 file changed +10
-22
lines changed
solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses Expand file tree Collapse file tree 1 file changed +10
-22
lines changed Original file line number Diff line number Diff line change 3
3
* @return {string }
4
4
*/
5
5
var reverseParentheses = function ( s ) {
6
- const n = s . length ;
7
- const d = new Array ( n ) . fill ( 0 ) ;
8
6
const stk = [ ] ;
9
- for ( let i = 0 ; i < n ; ++ i ) {
10
- if ( s [ i ] == '(' ) {
11
- stk . push ( i ) ;
12
- } else if ( s [ i ] == ')' ) {
13
- const j = stk . pop ( ) ;
14
- d [ i ] = j ;
15
- d [ j ] = i ;
16
- }
17
- }
18
- let i = 0 ;
19
- let x = 1 ;
20
- const ans = [ ] ;
21
- while ( i < n ) {
22
- const c = s . charAt ( i ) ;
23
- if ( c == '(' || c == ')' ) {
24
- i = d [ i ] ;
25
- x = - x ;
7
+ for ( const c of s ) {
8
+ if ( c === ')' ) {
9
+ const t = [ ] ;
10
+ while ( stk . at ( - 1 ) !== '(' ) {
11
+ t . push ( stk . pop ( ) ) ;
12
+ }
13
+ stk . pop ( ) ;
14
+ stk . push ( ...t ) ;
26
15
} else {
27
- ans . push ( c ) ;
16
+ stk . push ( c ) ;
28
17
}
29
- i += x ;
30
18
}
31
- return ans . join ( '' ) ;
19
+ return stk . join ( '' ) ;
32
20
} ;
You can’t perform that action at this time.
0 commit comments