Skip to content

Commit 37eb1d9

Browse files
authored
Update Solution.js
1 parent 1545d38 commit 37eb1d9

File tree

1 file changed

+10
-22
lines changed
  • solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses

1 file changed

+10
-22
lines changed

solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.js

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,18 @@
33
* @return {string}
44
*/
55
var reverseParentheses = function (s) {
6-
const n = s.length;
7-
const d = new Array(n).fill(0);
86
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);
2615
} else {
27-
ans.push(c);
16+
stk.push(c);
2817
}
29-
i += x;
3018
}
31-
return ans.join('');
19+
return stk.join('');
3220
};

0 commit comments

Comments
 (0)