From 3060c83239d8e39d94bb0ed2943129f906254a27 Mon Sep 17 00:00:00 2001 From: rain84 Date: Thu, 11 Jul 2024 17:06:55 +0300 Subject: [PATCH 01/14] feat: add ts solution to lc problem: No.1190 --- .../README.md | 33 +++++++++++++++++++ .../README_EN.md | 33 +++++++++++++++++++ .../Solution.ts | 28 ++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.ts diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README.md b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README.md index fc2e0e6677e9d..a3c9d78b7c74a 100644 --- a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README.md +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README.md @@ -214,6 +214,39 @@ var reverseParentheses = function (s) { }; ``` +#### JavaScript + +```ts +function reverseParentheses(s: string): string { + const n = s.length; + const d = new Array(n).fill(0); + const stk = []; + for (let i = 0; i < n; ++i) { + if (s[i] === '(') { + stk.push(i); + } else if (s[i] === ')') { + const j = stk.pop()!; + d[i] = j; + d[j] = i; + } + } + let i = 0; + let x = 1; + const ans = []; + while (i < n) { + const c = s.charAt(i); + if (c === '(' || c === ')') { + i = d[i]; + x = -x; + } else { + ans.push(c); + } + i += x; + } + return ans.join(''); +} +``` + diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README_EN.md b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README_EN.md index c0bbc391d7f41..05c14a4222983 100644 --- a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README_EN.md +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README_EN.md @@ -207,6 +207,39 @@ var reverseParentheses = function (s) { }; ``` +#### JavaScript + +```ts +function reverseParentheses(s: string): string { + const n = s.length; + const d = new Array(n).fill(0); + const stk = []; + for (let i = 0; i < n; ++i) { + if (s[i] === '(') { + stk.push(i); + } else if (s[i] === ')') { + const j = stk.pop()!; + d[i] = j; + d[j] = i; + } + } + let i = 0; + let x = 1; + const ans = []; + while (i < n) { + const c = s.charAt(i); + if (c === '(' || c === ')') { + i = d[i]; + x = -x; + } else { + ans.push(c); + } + i += x; + } + return ans.join(''); +} +``` + diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.ts b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.ts new file mode 100644 index 0000000000000..9ab2942d113b1 --- /dev/null +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.ts @@ -0,0 +1,28 @@ +function reverseParentheses(s: string): string { + const n = s.length; + const d = new Array(n).fill(0); + const stk = []; + for (let i = 0; i < n; ++i) { + if (s[i] === '(') { + stk.push(i); + } else if (s[i] === ')') { + const j = stk.pop()!; + d[i] = j; + d[j] = i; + } + } + let i = 0; + let x = 1; + const ans = []; + while (i < n) { + const c = s.charAt(i); + if (c === '(' || c === ')') { + i = d[i]; + x = -x; + } else { + ans.push(c); + } + i += x; + } + return ans.join(''); +} From 0a38ca6decc7faa5ef527c96a694ba4208a2623f Mon Sep 17 00:00:00 2001 From: rain84 Date: Thu, 11 Jul 2024 17:13:24 +0300 Subject: [PATCH 02/14] feat: add js solution to lc problem: No.1190 --- .../README.md | 38 +++++++++++++++++++ .../README_EN.md | 38 +++++++++++++++++++ .../Solution2.js | 33 ++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.js diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README.md b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README.md index a3c9d78b7c74a..e2b799f42de42 100644 --- a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README.md +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README.md @@ -357,6 +357,44 @@ func reverseParentheses(s string) string { } ``` +#### JavaScript + +```js +function reverseParentheses(s: string): string { + const res: string[] = []; + const n = s.length; + const pairs = Array(n).fill(-1); + const stack: number[] = []; + + for (let i = 0; i < n; i++) { + if (s[i] === '(') stack.push(i); + else if (s[i] === ')') { + const j = stack.pop()!; + pairs[i] = j; + pairs[j] = i; + } + } + + for (let i = 0, forward = true; i < n; ) { + const ch = s[i]; + + switch (s[i]) { + case '(': + case ')': + i = forward ? pairs[i] - 1 : pairs[i] + 1; + forward = !forward; + break; + + default: + res.push(ch); + i = forward ? i + 1 : i - 1; + } + } + + return res.join(''); +} +``` + diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README_EN.md b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README_EN.md index 05c14a4222983..735f2149997fc 100644 --- a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README_EN.md +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README_EN.md @@ -350,6 +350,44 @@ func reverseParentheses(s string) string { } ``` +#### JavaScript + +```js +function reverseParentheses(s: string): string { + const res: string[] = []; + const n = s.length; + const pairs = Array(n).fill(-1); + const stack: number[] = []; + + for (let i = 0; i < n; i++) { + if (s[i] === '(') stack.push(i); + else if (s[i] === ')') { + const j = stack.pop()!; + pairs[i] = j; + pairs[j] = i; + } + } + + for (let i = 0, forward = true; i < n; ) { + const ch = s[i]; + + switch (s[i]) { + case '(': + case ')': + i = forward ? pairs[i] - 1 : pairs[i] + 1; + forward = !forward; + break; + + default: + res.push(ch); + i = forward ? i + 1 : i - 1; + } + } + + return res.join(''); +} +``` + diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.js b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.js new file mode 100644 index 0000000000000..3997c337d7a89 --- /dev/null +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.js @@ -0,0 +1,33 @@ +function reverseParentheses(s) { + const res = []; + const n = s.length; + const pairs = Array(n).fill(-1); + const stack = []; + + for (let i = 0; i < n; i++) { + if (s[i] === '(') stack.push(i); + else if (s[i] === ')') { + const j = stack.pop(); + pairs[i] = j; + pairs[j] = i; + } + } + + for (let i = 0, forward = true; i < n; ) { + const ch = s[i]; + + switch (s[i]) { + case '(': + case ')': + i = forward ? pairs[i] - 1 : pairs[i] + 1; + forward = !forward; + break; + + default: + res.push(ch); + i = forward ? i + 1 : i - 1; + } + } + + return res.join(''); +} From 775cf7f37897e263d25e389568ef1391ceca13be Mon Sep 17 00:00:00 2001 From: rain84 Date: Thu, 11 Jul 2024 17:14:13 +0300 Subject: [PATCH 03/14] feat: add ts solution to lc problem: No.1190 --- .../README.md | 38 +++++++++++++++++++ .../README_EN.md | 38 +++++++++++++++++++ .../Solution2.ts | 33 ++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.ts diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README.md b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README.md index e2b799f42de42..36525ccf532c1 100644 --- a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README.md +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README.md @@ -395,6 +395,44 @@ function reverseParentheses(s: string): string { } ``` +#### TypeScript + +```ts +function reverseParentheses(s: string): string { + const res: string[] = []; + const n = s.length; + const pairs = Array(n).fill(-1); + const stack: number[] = []; + + for (let i = 0; i < n; i++) { + if (s[i] === '(') stack.push(i); + else if (s[i] === ')') { + const j = stack.pop()!; + pairs[i] = j; + pairs[j] = i; + } + } + + for (let i = 0, forward = true; i < n; ) { + const ch = s[i]; + + switch (s[i]) { + case '(': + case ')': + i = forward ? pairs[i] - 1 : pairs[i] + 1; + forward = !forward; + break; + + default: + res.push(ch); + i = forward ? i + 1 : i - 1; + } + } + + return res.join(''); +} +``` + diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README_EN.md b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README_EN.md index 735f2149997fc..8e2234270c33c 100644 --- a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README_EN.md +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README_EN.md @@ -388,6 +388,44 @@ function reverseParentheses(s: string): string { } ``` +#### TypeScript + +```ts +function reverseParentheses(s: string): string { + const res: string[] = []; + const n = s.length; + const pairs = Array(n).fill(-1); + const stack: number[] = []; + + for (let i = 0; i < n; i++) { + if (s[i] === '(') stack.push(i); + else if (s[i] === ')') { + const j = stack.pop()!; + pairs[i] = j; + pairs[j] = i; + } + } + + for (let i = 0, forward = true; i < n; ) { + const ch = s[i]; + + switch (s[i]) { + case '(': + case ')': + i = forward ? pairs[i] - 1 : pairs[i] + 1; + forward = !forward; + break; + + default: + res.push(ch); + i = forward ? i + 1 : i - 1; + } + } + + return res.join(''); +} +``` + diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.ts b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.ts new file mode 100644 index 0000000000000..d492e60a33ece --- /dev/null +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.ts @@ -0,0 +1,33 @@ +function reverseParentheses(s: string): string { + const res: string[] = []; + const n = s.length; + const pairs = Array(n).fill(-1); + const stack: number[] = []; + + for (let i = 0; i < n; i++) { + if (s[i] === '(') stack.push(i); + else if (s[i] === ')') { + const j = stack.pop()!; + pairs[i] = j; + pairs[j] = i; + } + } + + for (let i = 0, forward = true; i < n; ) { + const ch = s[i]; + + switch (s[i]) { + case '(': + case ')': + i = forward ? pairs[i] - 1 : pairs[i] + 1; + forward = !forward; + break; + + default: + res.push(ch); + i = forward ? i + 1 : i - 1; + } + } + + return res.join(''); +} From 85111babd0909e884cd8adefa9ffd1f939708083 Mon Sep 17 00:00:00 2001 From: rain84 Date: Thu, 11 Jul 2024 17:31:23 +0300 Subject: [PATCH 04/14] feat: update ts/js solutions to lc problem: No.1190 --- .../README.md | 38 +++++++++---------- .../README_EN.md | 38 +++++++++---------- .../Solution.ts | 4 +- .../Solution2.js | 14 +++---- .../Solution2.ts | 14 +++---- 5 files changed, 54 insertions(+), 54 deletions(-) diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README.md b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README.md index 36525ccf532c1..4970284d95a8d 100644 --- a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README.md +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README.md @@ -214,13 +214,13 @@ var reverseParentheses = function (s) { }; ``` -#### JavaScript +#### TypeScript ```ts function reverseParentheses(s: string): string { const n = s.length; const d = new Array(n).fill(0); - const stk = []; + const stk: number[] = []; for (let i = 0; i < n; ++i) { if (s[i] === '(') { stk.push(i); @@ -232,7 +232,7 @@ function reverseParentheses(s: string): string { } let i = 0; let x = 1; - const ans = []; + const ans: string[] = []; while (i < n) { const c = s.charAt(i); if (c === '(' || c === ')') { @@ -360,18 +360,18 @@ func reverseParentheses(s string) string { #### JavaScript ```js -function reverseParentheses(s: string): string { - const res: string[] = []; +function reverseParentheses(s) { + const res = []; const n = s.length; - const pairs = Array(n).fill(-1); - const stack: number[] = []; + const d = Array(n).fill(-1); + const stk = []; for (let i = 0; i < n; i++) { - if (s[i] === '(') stack.push(i); + if (s[i] === '(') stk.push(i); else if (s[i] === ')') { - const j = stack.pop()!; - pairs[i] = j; - pairs[j] = i; + const j = stk.pop(); + d[i] = j; + d[j] = i; } } @@ -381,7 +381,7 @@ function reverseParentheses(s: string): string { switch (s[i]) { case '(': case ')': - i = forward ? pairs[i] - 1 : pairs[i] + 1; + i = forward ? d[i] - 1 : d[i] + 1; forward = !forward; break; @@ -401,15 +401,15 @@ function reverseParentheses(s: string): string { function reverseParentheses(s: string): string { const res: string[] = []; const n = s.length; - const pairs = Array(n).fill(-1); - const stack: number[] = []; + const d = Array(n).fill(-1); + const stk: number[] = []; for (let i = 0; i < n; i++) { - if (s[i] === '(') stack.push(i); + if (s[i] === '(') stk.push(i); else if (s[i] === ')') { - const j = stack.pop()!; - pairs[i] = j; - pairs[j] = i; + const j = stk.pop()!; + d[i] = j; + d[j] = i; } } @@ -419,7 +419,7 @@ function reverseParentheses(s: string): string { switch (s[i]) { case '(': case ')': - i = forward ? pairs[i] - 1 : pairs[i] + 1; + i = forward ? d[i] - 1 : d[i] + 1; forward = !forward; break; diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README_EN.md b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README_EN.md index 8e2234270c33c..08c02bbede8c2 100644 --- a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README_EN.md +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README_EN.md @@ -207,13 +207,13 @@ var reverseParentheses = function (s) { }; ``` -#### JavaScript +#### TypeScript ```ts function reverseParentheses(s: string): string { const n = s.length; const d = new Array(n).fill(0); - const stk = []; + const stk: number[] = []; for (let i = 0; i < n; ++i) { if (s[i] === '(') { stk.push(i); @@ -225,7 +225,7 @@ function reverseParentheses(s: string): string { } let i = 0; let x = 1; - const ans = []; + const ans: string[] = []; while (i < n) { const c = s.charAt(i); if (c === '(' || c === ')') { @@ -353,18 +353,18 @@ func reverseParentheses(s string) string { #### JavaScript ```js -function reverseParentheses(s: string): string { - const res: string[] = []; +function reverseParentheses(s) { + const res = []; const n = s.length; - const pairs = Array(n).fill(-1); - const stack: number[] = []; + const d = Array(n).fill(-1); + const stk = []; for (let i = 0; i < n; i++) { - if (s[i] === '(') stack.push(i); + if (s[i] === '(') stk.push(i); else if (s[i] === ')') { - const j = stack.pop()!; - pairs[i] = j; - pairs[j] = i; + const j = stk.pop(); + d[i] = j; + d[j] = i; } } @@ -374,7 +374,7 @@ function reverseParentheses(s: string): string { switch (s[i]) { case '(': case ')': - i = forward ? pairs[i] - 1 : pairs[i] + 1; + i = forward ? d[i] - 1 : d[i] + 1; forward = !forward; break; @@ -394,15 +394,15 @@ function reverseParentheses(s: string): string { function reverseParentheses(s: string): string { const res: string[] = []; const n = s.length; - const pairs = Array(n).fill(-1); - const stack: number[] = []; + const d = Array(n).fill(-1); + const stk: number[] = []; for (let i = 0; i < n; i++) { - if (s[i] === '(') stack.push(i); + if (s[i] === '(') stk.push(i); else if (s[i] === ')') { - const j = stack.pop()!; - pairs[i] = j; - pairs[j] = i; + const j = stk.pop()!; + d[i] = j; + d[j] = i; } } @@ -412,7 +412,7 @@ function reverseParentheses(s: string): string { switch (s[i]) { case '(': case ')': - i = forward ? pairs[i] - 1 : pairs[i] + 1; + i = forward ? d[i] - 1 : d[i] + 1; forward = !forward; break; diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.ts b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.ts index 9ab2942d113b1..4916ee6d65ad5 100644 --- a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.ts +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.ts @@ -1,7 +1,7 @@ function reverseParentheses(s: string): string { const n = s.length; const d = new Array(n).fill(0); - const stk = []; + const stk: number[] = []; for (let i = 0; i < n; ++i) { if (s[i] === '(') { stk.push(i); @@ -13,7 +13,7 @@ function reverseParentheses(s: string): string { } let i = 0; let x = 1; - const ans = []; + const ans: string[] = []; while (i < n) { const c = s.charAt(i); if (c === '(' || c === ')') { diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.js b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.js index 3997c337d7a89..cbc86e3f0b401 100644 --- a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.js +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.js @@ -1,15 +1,15 @@ function reverseParentheses(s) { const res = []; const n = s.length; - const pairs = Array(n).fill(-1); - const stack = []; + const d = Array(n).fill(-1); + const stk = []; for (let i = 0; i < n; i++) { - if (s[i] === '(') stack.push(i); + if (s[i] === '(') stk.push(i); else if (s[i] === ')') { - const j = stack.pop(); - pairs[i] = j; - pairs[j] = i; + const j = stk.pop(); + d[i] = j; + d[j] = i; } } @@ -19,7 +19,7 @@ function reverseParentheses(s) { switch (s[i]) { case '(': case ')': - i = forward ? pairs[i] - 1 : pairs[i] + 1; + i = forward ? d[i] - 1 : d[i] + 1; forward = !forward; break; diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.ts b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.ts index d492e60a33ece..1202c315c87dc 100644 --- a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.ts +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.ts @@ -1,15 +1,15 @@ function reverseParentheses(s: string): string { const res: string[] = []; const n = s.length; - const pairs = Array(n).fill(-1); - const stack: number[] = []; + const d = Array(n).fill(-1); + const stk: number[] = []; for (let i = 0; i < n; i++) { - if (s[i] === '(') stack.push(i); + if (s[i] === '(') stk.push(i); else if (s[i] === ')') { - const j = stack.pop()!; - pairs[i] = j; - pairs[j] = i; + const j = stk.pop()!; + d[i] = j; + d[j] = i; } } @@ -19,7 +19,7 @@ function reverseParentheses(s: string): string { switch (s[i]) { case '(': case ')': - i = forward ? pairs[i] - 1 : pairs[i] + 1; + i = forward ? d[i] - 1 : d[i] + 1; forward = !forward; break; From 64942879554e6fad436958eb0f1ddbeaf4458f26 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 12 Jul 2024 19:12:06 +0800 Subject: [PATCH 05/14] Update README.md --- .../README.md | 276 +++++++++--------- 1 file changed, 135 insertions(+), 141 deletions(-) diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README.md b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README.md index 4970284d95a8d..7547ec2bfc4dd 100644 --- a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README.md +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README.md @@ -73,9 +73,9 @@ tags: ### 方法一:模拟 -用双端队列或者栈,模拟反转的过程。 +我们可以直接用栈来模拟反转的过程。 -时间复杂度 $O(n^2)$,其中 $n$ 为字符串 $s$ 的长度。 +时间复杂度 $O(n^2)$,空间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。 @@ -86,15 +86,15 @@ class Solution: def reverseParentheses(self, s: str) -> str: stk = [] for c in s: - if c == ')': + if c == ")": t = [] - while stk[-1] != '(': + while stk[-1] != "(": t.append(stk.pop()) stk.pop() stk.extend(t) else: stk.append(c) - return ''.join(stk) + return "".join(stk) ``` #### Java @@ -102,30 +102,21 @@ class Solution: ```java class Solution { public String reverseParentheses(String s) { - int n = s.length(); - int[] d = new int[n]; - Deque stk = new ArrayDeque<>(); - for (int i = 0; i < n; ++i) { - if (s.charAt(i) == '(') { - stk.push(i); - } else if (s.charAt(i) == ')') { - int j = stk.pop(); - d[i] = j; - d[j] = i; - } - } - StringBuilder ans = new StringBuilder(); - int i = 0, x = 1; - while (i < n) { - if (s.charAt(i) == '(' || s.charAt(i) == ')') { - i = d[i]; - x = -x; + StringBuilder stk = new StringBuilder(); + for (char c : s.toCharArray()) { + if (c == ')') { + StringBuilder t = new StringBuilder(); + while (stk.charAt(stk.length() - 1) != '(') { + t.append(stk.charAt(stk.length() - 1)); + stk.deleteCharAt(stk.length() - 1); + } + stk.deleteCharAt(stk.length() - 1); + stk.append(t); } else { - ans.append(s.charAt(i)); + stk.append(c); } - i += x; } - return ans.toString(); + return stk.toString(); } } ``` @@ -177,6 +168,27 @@ func reverseParentheses(s string) string { } ``` +#### TypeScript + +```ts +function reverseParentheses(s: string): string { + const stk: string[] = []; + for (const c of s) { + if (c === ')') { + const t: string[] = []; + while (stk.at(-1)! !== '(') { + t.push(stk.pop()!); + } + stk.pop(); + stk.push(...t); + } else { + stk.push(c); + } + } + return stk.join(''); +} +``` + #### JavaScript ```js @@ -185,68 +197,23 @@ func reverseParentheses(s string) string { * @return {string} */ var reverseParentheses = function (s) { - const n = s.length; - const d = new Array(n).fill(0); const stk = []; - for (let i = 0; i < n; ++i) { - if (s[i] == '(') { - stk.push(i); - } else if (s[i] == ')') { - const j = stk.pop(); - d[i] = j; - d[j] = i; - } - } - let i = 0; - let x = 1; - const ans = []; - while (i < n) { - const c = s.charAt(i); - if (c == '(' || c == ')') { - i = d[i]; - x = -x; + for (const c of s) { + if (c === ')') { + const t = []; + while (stk.at(-1) !== '(') { + t.push(stk.pop()); + } + stk.pop(); + stk.push(...t); } else { - ans.push(c); + stk.push(c); } - i += x; } - return ans.join(''); + return stk.join(''); }; ``` -#### TypeScript - -```ts -function reverseParentheses(s: string): string { - const n = s.length; - const d = new Array(n).fill(0); - const stk: number[] = []; - for (let i = 0; i < n; ++i) { - if (s[i] === '(') { - stk.push(i); - } else if (s[i] === ')') { - const j = stk.pop()!; - d[i] = j; - d[j] = i; - } - } - let i = 0; - let x = 1; - const ans: string[] = []; - while (i < n) { - const c = s.charAt(i); - if (c === '(' || c === ')') { - i = d[i]; - x = -x; - } else { - ans.push(c); - } - i += x; - } - return ans.join(''); -} -``` - @@ -261,7 +228,7 @@ function reverseParentheses(s: string): string { 然后,我们从左到右遍历字符串,遇到 `(` 或者 `)` 时,根据 $d$ 数组跳到对应的位置,然后反转方向,继续遍历,直到遍历完整个字符串。 -时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。 +时间复杂度 $O(n)$,空间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。 @@ -274,21 +241,54 @@ class Solution: d = [0] * n stk = [] for i, c in enumerate(s): - if c == '(': + if c == "(": stk.append(i) - elif c == ')': + elif c == ")": j = stk.pop() d[i], d[j] = j, i i, x = 0, 1 ans = [] while i < n: - if s[i] in '()': + if s[i] in "()": i = d[i] x = -x else: ans.append(s[i]) i += x - return ''.join(ans) + return "".join(ans) +``` + +#### Java + +```java +class Solution { + public String reverseParentheses(String s) { + int n = s.length(); + int[] d = new int[n]; + Deque stk = new ArrayDeque<>(); + for (int i = 0; i < n; ++i) { + if (s.charAt(i) == '(') { + stk.push(i); + } else if (s.charAt(i) == ')') { + int j = stk.pop(); + d[i] = j; + d[j] = i; + } + } + StringBuilder ans = new StringBuilder(); + int i = 0, x = 1; + while (i < n) { + if (s.charAt(i) == '(' || s.charAt(i) == ')') { + i = d[i]; + x = -x; + } else { + ans.append(s.charAt(i)); + } + i += x; + } + return ans.toString(); + } +} ``` #### C++ @@ -357,80 +357,74 @@ func reverseParentheses(s string) string { } ``` -#### JavaScript +#### TypeScript -```js -function reverseParentheses(s) { - const res = []; +```ts +function reverseParentheses(s: string): string { const n = s.length; - const d = Array(n).fill(-1); - const stk = []; - - for (let i = 0; i < n; i++) { - if (s[i] === '(') stk.push(i); - else if (s[i] === ')') { - const j = stk.pop(); + const d: number[] = Array(n).fill(0); + const stk: number[] = []; + for (let i = 0; i < n; ++i) { + if (s[i] === '(') { + stk.push(i); + } else if (s[i] === ')') { + const j = stk.pop()!; d[i] = j; d[j] = i; } } - - for (let i = 0, forward = true; i < n; ) { - const ch = s[i]; - - switch (s[i]) { - case '(': - case ')': - i = forward ? d[i] - 1 : d[i] + 1; - forward = !forward; - break; - - default: - res.push(ch); - i = forward ? i + 1 : i - 1; + let i = 0; + let x = 1; + const ans: string[] = []; + while (i < n) { + const c = s.charAt(i); + if ('()'.includes(c)) { + i = d[i]; + x = -x; + } else { + ans.push(c); } + i += x; } - - return res.join(''); + return ans.join(''); } ``` -#### TypeScript +#### JavaScript -```ts -function reverseParentheses(s: string): string { - const res: string[] = []; +```js +/** + * @param {string} s + * @return {string} + */ +var reverseParentheses = function (s) { const n = s.length; - const d = Array(n).fill(-1); - const stk: number[] = []; - - for (let i = 0; i < n; i++) { - if (s[i] === '(') stk.push(i); - else if (s[i] === ')') { - const j = stk.pop()!; + const d = Array(n).fill(0); + const stk = []; + for (let i = 0; i < n; ++i) { + if (s[i] === '(') { + stk.push(i); + } else if (s[i] === ')') { + const j = stk.pop(); d[i] = j; d[j] = i; } } - - for (let i = 0, forward = true; i < n; ) { - const ch = s[i]; - - switch (s[i]) { - case '(': - case ')': - i = forward ? d[i] - 1 : d[i] + 1; - forward = !forward; - break; - - default: - res.push(ch); - i = forward ? i + 1 : i - 1; + let i = 0; + let x = 1; + const ans = []; + while (i < n) { + const c = s.charAt(i); + if ('()'.includes(c)) { + i = d[i]; + x = -x; + } else { + ans.push(c); } + i += x; } - - return res.join(''); -} + return ans.join(''); +}; ``` From e265658982bb1e07f7070e88039068993d43e656 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 12 Jul 2024 19:12:17 +0800 Subject: [PATCH 06/14] Update README_EN.md --- .../README_EN.md | 284 +++++++++--------- 1 file changed, 139 insertions(+), 145 deletions(-) diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README_EN.md b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README_EN.md index 08c02bbede8c2..bb301d2ba6bc4 100644 --- a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README_EN.md +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README_EN.md @@ -66,9 +66,9 @@ tags: ### Solution 1: Simulation -We can use a double-ended queue or stack to simulate the reversal process. +We can directly use a stack to simulate the reversal process. -The time complexity is $O(n^2)$, where $n$ is the length of the string $s$. +The time complexity is $O(n^2)$, and the space complexity is $O(n)$, where $n$ is the length of the string $s$. @@ -79,15 +79,15 @@ class Solution: def reverseParentheses(self, s: str) -> str: stk = [] for c in s: - if c == ')': + if c == ")": t = [] - while stk[-1] != '(': + while stk[-1] != "(": t.append(stk.pop()) stk.pop() stk.extend(t) else: stk.append(c) - return ''.join(stk) + return "".join(stk) ``` #### Java @@ -95,30 +95,21 @@ class Solution: ```java class Solution { public String reverseParentheses(String s) { - int n = s.length(); - int[] d = new int[n]; - Deque stk = new ArrayDeque<>(); - for (int i = 0; i < n; ++i) { - if (s.charAt(i) == '(') { - stk.push(i); - } else if (s.charAt(i) == ')') { - int j = stk.pop(); - d[i] = j; - d[j] = i; - } - } - StringBuilder ans = new StringBuilder(); - int i = 0, x = 1; - while (i < n) { - if (s.charAt(i) == '(' || s.charAt(i) == ')') { - i = d[i]; - x = -x; + StringBuilder stk = new StringBuilder(); + for (char c : s.toCharArray()) { + if (c == ')') { + StringBuilder t = new StringBuilder(); + while (stk.charAt(stk.length() - 1) != '(') { + t.append(stk.charAt(stk.length() - 1)); + stk.deleteCharAt(stk.length() - 1); + } + stk.deleteCharAt(stk.length() - 1); + stk.append(t); } else { - ans.append(s.charAt(i)); + stk.append(c); } - i += x; } - return ans.toString(); + return stk.toString(); } } ``` @@ -170,6 +161,27 @@ func reverseParentheses(s string) string { } ``` +#### TypeScript + +```ts +function reverseParentheses(s: string): string { + const stk: string[] = []; + for (const c of s) { + if (c === ')') { + const t: string[] = []; + while (stk.at(-1)! !== '(') { + t.push(stk.pop()!); + } + stk.pop(); + stk.push(...t); + } else { + stk.push(c); + } + } + return stk.join(''); +} +``` + #### JavaScript ```js @@ -178,83 +190,38 @@ func reverseParentheses(s string) string { * @return {string} */ var reverseParentheses = function (s) { - const n = s.length; - const d = new Array(n).fill(0); const stk = []; - for (let i = 0; i < n; ++i) { - if (s[i] == '(') { - stk.push(i); - } else if (s[i] == ')') { - const j = stk.pop(); - d[i] = j; - d[j] = i; - } - } - let i = 0; - let x = 1; - const ans = []; - while (i < n) { - const c = s.charAt(i); - if (c == '(' || c == ')') { - i = d[i]; - x = -x; + for (const c of s) { + if (c === ')') { + const t = []; + while (stk.at(-1) !== '(') { + t.push(stk.pop()); + } + stk.pop(); + stk.push(...t); } else { - ans.push(c); + stk.push(c); } - i += x; } - return ans.join(''); + return stk.join(''); }; ``` -#### TypeScript - -```ts -function reverseParentheses(s: string): string { - const n = s.length; - const d = new Array(n).fill(0); - const stk: number[] = []; - for (let i = 0; i < n; ++i) { - if (s[i] === '(') { - stk.push(i); - } else if (s[i] === ')') { - const j = stk.pop()!; - d[i] = j; - d[j] = i; - } - } - let i = 0; - let x = 1; - const ans: string[] = []; - while (i < n) { - const c = s.charAt(i); - if (c === '(' || c === ')') { - i = d[i]; - x = -x; - } else { - ans.push(c); - } - i += x; - } - return ans.join(''); -} -``` - -### Solution 2: Quick Thinking +### Solution 2: Brain Teaser -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. +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. -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$. +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$. -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. +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. -The time complexity is $O(n)$, where $n$ is the length of the string $s$. +The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the string $s$. @@ -267,21 +234,54 @@ class Solution: d = [0] * n stk = [] for i, c in enumerate(s): - if c == '(': + if c == "(": stk.append(i) - elif c == ')': + elif c == ")": j = stk.pop() d[i], d[j] = j, i i, x = 0, 1 ans = [] while i < n: - if s[i] in '()': + if s[i] in "()": i = d[i] x = -x else: ans.append(s[i]) i += x - return ''.join(ans) + return "".join(ans) +``` + +#### Java + +```java +class Solution { + public String reverseParentheses(String s) { + int n = s.length(); + int[] d = new int[n]; + Deque stk = new ArrayDeque<>(); + for (int i = 0; i < n; ++i) { + if (s.charAt(i) == '(') { + stk.push(i); + } else if (s.charAt(i) == ')') { + int j = stk.pop(); + d[i] = j; + d[j] = i; + } + } + StringBuilder ans = new StringBuilder(); + int i = 0, x = 1; + while (i < n) { + if (s.charAt(i) == '(' || s.charAt(i) == ')') { + i = d[i]; + x = -x; + } else { + ans.append(s.charAt(i)); + } + i += x; + } + return ans.toString(); + } +} ``` #### C++ @@ -350,80 +350,74 @@ func reverseParentheses(s string) string { } ``` -#### JavaScript +#### TypeScript -```js -function reverseParentheses(s) { - const res = []; +```ts +function reverseParentheses(s: string): string { const n = s.length; - const d = Array(n).fill(-1); - const stk = []; - - for (let i = 0; i < n; i++) { - if (s[i] === '(') stk.push(i); - else if (s[i] === ')') { - const j = stk.pop(); + const d: number[] = Array(n).fill(0); + const stk: number[] = []; + for (let i = 0; i < n; ++i) { + if (s[i] === '(') { + stk.push(i); + } else if (s[i] === ')') { + const j = stk.pop()!; d[i] = j; d[j] = i; } } - - for (let i = 0, forward = true; i < n; ) { - const ch = s[i]; - - switch (s[i]) { - case '(': - case ')': - i = forward ? d[i] - 1 : d[i] + 1; - forward = !forward; - break; - - default: - res.push(ch); - i = forward ? i + 1 : i - 1; + let i = 0; + let x = 1; + const ans: string[] = []; + while (i < n) { + const c = s.charAt(i); + if ('()'.includes(c)) { + i = d[i]; + x = -x; + } else { + ans.push(c); } + i += x; } - - return res.join(''); + return ans.join(''); } ``` -#### TypeScript +#### JavaScript -```ts -function reverseParentheses(s: string): string { - const res: string[] = []; +```js +/** + * @param {string} s + * @return {string} + */ +var reverseParentheses = function (s) { const n = s.length; - const d = Array(n).fill(-1); - const stk: number[] = []; - - for (let i = 0; i < n; i++) { - if (s[i] === '(') stk.push(i); - else if (s[i] === ')') { - const j = stk.pop()!; + const d = Array(n).fill(0); + const stk = []; + for (let i = 0; i < n; ++i) { + if (s[i] === '(') { + stk.push(i); + } else if (s[i] === ')') { + const j = stk.pop(); d[i] = j; d[j] = i; } } - - for (let i = 0, forward = true; i < n; ) { - const ch = s[i]; - - switch (s[i]) { - case '(': - case ')': - i = forward ? d[i] - 1 : d[i] + 1; - forward = !forward; - break; - - default: - res.push(ch); - i = forward ? i + 1 : i - 1; + let i = 0; + let x = 1; + const ans = []; + while (i < n) { + const c = s.charAt(i); + if ('()'.includes(c)) { + i = d[i]; + x = -x; + } else { + ans.push(c); } + i += x; } - - return res.join(''); -} + return ans.join(''); +}; ``` From 1545d386e5926bcf28b36e867c816df6fe863bea Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 12 Jul 2024 19:13:01 +0800 Subject: [PATCH 07/14] Update Solution.java --- .../Solution.java | 35 +++++++------------ 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.java b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.java index 8a1e4cdd0febd..6c026fc98e8a5 100644 --- a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.java +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.java @@ -1,28 +1,19 @@ class Solution { public String reverseParentheses(String s) { - int n = s.length(); - int[] d = new int[n]; - Deque stk = new ArrayDeque<>(); - for (int i = 0; i < n; ++i) { - if (s.charAt(i) == '(') { - stk.push(i); - } else if (s.charAt(i) == ')') { - int j = stk.pop(); - d[i] = j; - d[j] = i; - } - } - StringBuilder ans = new StringBuilder(); - int i = 0, x = 1; - while (i < n) { - if (s.charAt(i) == '(' || s.charAt(i) == ')') { - i = d[i]; - x = -x; + StringBuilder stk = new StringBuilder(); + for (char c : s.toCharArray()) { + if (c == ')') { + StringBuilder t = new StringBuilder(); + while (stk.charAt(stk.length() - 1) != '(') { + t.append(stk.charAt(stk.length() - 1)); + stk.deleteCharAt(stk.length() - 1); + } + stk.deleteCharAt(stk.length() - 1); + stk.append(t); } else { - ans.append(s.charAt(i)); + stk.append(c); } - i += x; } - return ans.toString(); + return stk.toString(); } -} \ No newline at end of file +} From 37eb1d9cd997d85e4370e4edf17e9c6074d6e793 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 12 Jul 2024 19:13:11 +0800 Subject: [PATCH 08/14] Update Solution.js --- .../Solution.js | 32 ++++++------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.js b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.js index 8bfeb64750670..15c60516dc61a 100644 --- a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.js +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.js @@ -3,30 +3,18 @@ * @return {string} */ var reverseParentheses = function (s) { - const n = s.length; - const d = new Array(n).fill(0); const stk = []; - for (let i = 0; i < n; ++i) { - if (s[i] == '(') { - stk.push(i); - } else if (s[i] == ')') { - const j = stk.pop(); - d[i] = j; - d[j] = i; - } - } - let i = 0; - let x = 1; - const ans = []; - while (i < n) { - const c = s.charAt(i); - if (c == '(' || c == ')') { - i = d[i]; - x = -x; + for (const c of s) { + if (c === ')') { + const t = []; + while (stk.at(-1) !== '(') { + t.push(stk.pop()); + } + stk.pop(); + stk.push(...t); } else { - ans.push(c); + stk.push(c); } - i += x; } - return ans.join(''); + return stk.join(''); }; From 012c46d09aa24c3b1e8d820c7743c09c490af0e0 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 12 Jul 2024 19:13:22 +0800 Subject: [PATCH 09/14] Update Solution.py --- .../Solution.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.py b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.py index 7ae106bbea4a0..1fda300c41f2e 100644 --- a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.py +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.py @@ -2,12 +2,12 @@ class Solution: def reverseParentheses(self, s: str) -> str: stk = [] for c in s: - if c == ')': + if c == ")": t = [] - while stk[-1] != '(': + while stk[-1] != "(": t.append(stk.pop()) stk.pop() stk.extend(t) else: stk.append(c) - return ''.join(stk) + return "".join(stk) From 45bd633136620ea82b6f040415b5347cde476744 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 12 Jul 2024 19:13:32 +0800 Subject: [PATCH 10/14] Update Solution.ts --- .../Solution.ts | 34 ++++++------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.ts b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.ts index 4916ee6d65ad5..3cd893f9a507d 100644 --- a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.ts +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution.ts @@ -1,28 +1,16 @@ function reverseParentheses(s: string): string { - const n = s.length; - const d = new Array(n).fill(0); - const stk: number[] = []; - for (let i = 0; i < n; ++i) { - if (s[i] === '(') { - stk.push(i); - } else if (s[i] === ')') { - const j = stk.pop()!; - d[i] = j; - d[j] = i; - } - } - let i = 0; - let x = 1; - const ans: string[] = []; - while (i < n) { - const c = s.charAt(i); - if (c === '(' || c === ')') { - i = d[i]; - x = -x; + const stk: string[] = []; + for (const c of s) { + if (c === ')') { + const t: string[] = []; + while (stk.at(-1)! !== '(') { + t.push(stk.pop()!); + } + stk.pop(); + stk.push(...t); } else { - ans.push(c); + stk.push(c); } - i += x; } - return ans.join(''); + return stk.join(''); } From a3c9b3978682489b34ab708a784fa3bfcfb38deb Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 12 Jul 2024 19:13:46 +0800 Subject: [PATCH 11/14] Create Solution2.java --- .../Solution2.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.java diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.java b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.java new file mode 100644 index 0000000000000..aba36d74fc0ce --- /dev/null +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.java @@ -0,0 +1,28 @@ +class Solution { + public String reverseParentheses(String s) { + int n = s.length(); + int[] d = new int[n]; + Deque stk = new ArrayDeque<>(); + for (int i = 0; i < n; ++i) { + if (s.charAt(i) == '(') { + stk.push(i); + } else if (s.charAt(i) == ')') { + int j = stk.pop(); + d[i] = j; + d[j] = i; + } + } + StringBuilder ans = new StringBuilder(); + int i = 0, x = 1; + while (i < n) { + if (s.charAt(i) == '(' || s.charAt(i) == ')') { + i = d[i]; + x = -x; + } else { + ans.append(s.charAt(i)); + } + i += x; + } + return ans.toString(); + } +} From ad7c0bd867ebaa9a8880450654d03d752058eccb Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 12 Jul 2024 19:13:58 +0800 Subject: [PATCH 12/14] Update Solution2.js --- .../Solution2.js | 47 +++++++++---------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.js b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.js index cbc86e3f0b401..5cec3ddb5f95e 100644 --- a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.js +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.js @@ -1,33 +1,32 @@ -function reverseParentheses(s) { - const res = []; +/** + * @param {string} s + * @return {string} + */ +var reverseParentheses = function (s) { const n = s.length; - const d = Array(n).fill(-1); + const d = Array(n).fill(0); const stk = []; - - for (let i = 0; i < n; i++) { - if (s[i] === '(') stk.push(i); - else if (s[i] === ')') { + for (let i = 0; i < n; ++i) { + if (s[i] === '(') { + stk.push(i); + } else if (s[i] === ')') { const j = stk.pop(); d[i] = j; d[j] = i; } } - - for (let i = 0, forward = true; i < n; ) { - const ch = s[i]; - - switch (s[i]) { - case '(': - case ')': - i = forward ? d[i] - 1 : d[i] + 1; - forward = !forward; - break; - - default: - res.push(ch); - i = forward ? i + 1 : i - 1; + let i = 0; + let x = 1; + const ans = []; + while (i < n) { + const c = s.charAt(i); + if ('()'.includes(c)) { + i = d[i]; + x = -x; + } else { + ans.push(c); } + i += x; } - - return res.join(''); -} + return ans.join(''); +}; From 91a21cc74f5ac3aec415605d39d1856aea9bdb50 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 12 Jul 2024 19:14:07 +0800 Subject: [PATCH 13/14] Update Solution2.py --- .../Solution2.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.py b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.py index dbd8e08776a9b..38916c562da31 100644 --- a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.py +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.py @@ -4,18 +4,18 @@ def reverseParentheses(self, s: str) -> str: d = [0] * n stk = [] for i, c in enumerate(s): - if c == '(': + if c == "(": stk.append(i) - elif c == ')': + elif c == ")": j = stk.pop() d[i], d[j] = j, i i, x = 0, 1 ans = [] while i < n: - if s[i] in '()': + if s[i] in "()": i = d[i] x = -x else: ans.append(s[i]) i += x - return ''.join(ans) + return "".join(ans) From bcd8786158e409bd578358194772ed9d98105780 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 12 Jul 2024 19:14:15 +0800 Subject: [PATCH 14/14] Update Solution2.ts --- .../Solution2.ts | 39 ++++++++----------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.ts b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.ts index 1202c315c87dc..dd18a93202384 100644 --- a/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.ts +++ b/solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.ts @@ -1,33 +1,28 @@ function reverseParentheses(s: string): string { - const res: string[] = []; const n = s.length; - const d = Array(n).fill(-1); + const d: number[] = Array(n).fill(0); const stk: number[] = []; - - for (let i = 0; i < n; i++) { - if (s[i] === '(') stk.push(i); - else if (s[i] === ')') { + for (let i = 0; i < n; ++i) { + if (s[i] === '(') { + stk.push(i); + } else if (s[i] === ')') { const j = stk.pop()!; d[i] = j; d[j] = i; } } - - for (let i = 0, forward = true; i < n; ) { - const ch = s[i]; - - switch (s[i]) { - case '(': - case ')': - i = forward ? d[i] - 1 : d[i] + 1; - forward = !forward; - break; - - default: - res.push(ch); - i = forward ? i + 1 : i - 1; + let i = 0; + let x = 1; + const ans: string[] = []; + while (i < n) { + const c = s.charAt(i); + if ('()'.includes(c)) { + i = d[i]; + x = -x; + } else { + ans.push(c); } + i += x; } - - return res.join(''); + return ans.join(''); }