File tree 3 files changed +109
-0
lines changed
solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses
3 files changed +109
-0
lines changed Original file line number Diff line number Diff line change @@ -108,6 +108,44 @@ class Solution {
108
108
}
109
109
```
110
110
111
+ ### ** JavaScript**
112
+
113
+ ``` js
114
+ /**
115
+ * @param {string} s
116
+ * @return {string}
117
+ */
118
+ var reverseParentheses = function (s ) {
119
+ let stack = [];
120
+ let hashMap = {};
121
+ const n = s .length ;
122
+ for (let i = 0 ; i < n; i++ ) {
123
+ let cur = s .charAt (i);
124
+ if (cur == ' (' ) {
125
+ stack .push (i);
126
+ } else if (cur == ' )' ) {
127
+ let left = stack .pop ();
128
+ hashMap[left] = i;
129
+ hashMap[i] = left;
130
+ }
131
+ }
132
+ let res = [];
133
+ let i = 0 ;
134
+ let step = 1 ; // 1向右,-1向左
135
+ while (i > - 1 && i < n) {
136
+ let cur = s .charAt (i);
137
+ if (cur == ' (' || cur == ' )' ) {
138
+ step = - step;
139
+ i = hashMap[i];
140
+ } else {
141
+ res .push (cur);
142
+ }
143
+ i += step;
144
+ }
145
+ return res .join (' ' );
146
+ };
147
+ ```
148
+
111
149
### ** ...**
112
150
113
151
```
Original file line number Diff line number Diff line change @@ -104,6 +104,44 @@ class Solution {
104
104
}
105
105
```
106
106
107
+ ### ** JavaScript**
108
+
109
+ ``` js
110
+ /**
111
+ * @param {string} s
112
+ * @return {string}
113
+ */
114
+ var reverseParentheses = function (s ) {
115
+ let stack = [];
116
+ let hashMap = {};
117
+ const n = s .length ;
118
+ for (let i = 0 ; i < n; i++ ) {
119
+ let cur = s .charAt (i);
120
+ if (cur == ' (' ) {
121
+ stack .push (i);
122
+ } else if (cur == ' )' ) {
123
+ let left = stack .pop ();
124
+ hashMap[left] = i;
125
+ hashMap[i] = left;
126
+ }
127
+ }
128
+ let res = [];
129
+ let i = 0 ;
130
+ let step = 1 ; // 1向右,-1向左
131
+ while (i > - 1 && i < n) {
132
+ let cur = s .charAt (i);
133
+ if (cur == ' (' || cur == ' )' ) {
134
+ step = - step;
135
+ i = hashMap[i];
136
+ } else {
137
+ res .push (cur);
138
+ }
139
+ i += step;
140
+ }
141
+ return res .join (' ' );
142
+ };
143
+ ```
144
+
107
145
### ** ...**
108
146
109
147
```
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } s
3
+ * @return {string }
4
+ */
5
+ var reverseParentheses = function ( s ) {
6
+ let stack = [ ] ;
7
+ let hashMap = { } ;
8
+ const n = s . length ;
9
+ for ( let i = 0 ; i < n ; i ++ ) {
10
+ let cur = s . charAt ( i ) ;
11
+ if ( cur == '(' ) {
12
+ stack . push ( i ) ;
13
+ } else if ( cur == ')' ) {
14
+ let left = stack . pop ( ) ;
15
+ hashMap [ left ] = i ;
16
+ hashMap [ i ] = left ;
17
+ }
18
+ }
19
+ let res = [ ] ;
20
+ let i = 0 ;
21
+ let step = 1 ; // 1向右,-1向左
22
+ while ( i > - 1 && i < n ) {
23
+ let cur = s . charAt ( i ) ;
24
+ if ( cur == '(' || cur == ')' ) {
25
+ step = - step ;
26
+ i = hashMap [ i ] ;
27
+ } else {
28
+ res . push ( cur ) ;
29
+ }
30
+ i += step ;
31
+ }
32
+ return res . join ( '' ) ;
33
+ } ;
You can’t perform that action at this time.
0 commit comments