Skip to content

Commit 6494287

Browse files
authored
Update README.md
1 parent 85111ba commit 6494287

File tree

1 file changed

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

1 file changed

+135
-141
lines changed

solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README.md

Lines changed: 135 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ tags:
7373

7474
### 方法一:模拟
7575

76-
用双端队列或者栈,模拟反转的过程
76+
我们可以直接用栈来模拟反转的过程
7777

78-
时间复杂度 $O(n^2)$,其中 $n$ 为字符串 $s$ 的长度。
78+
时间复杂度 $O(n^2)$,空间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。
7979

8080
<!-- tabs:start -->
8181

@@ -86,46 +86,37 @@ class Solution:
8686
def reverseParentheses(self, s: str) -> str:
8787
stk = []
8888
for c in s:
89-
if c == ')':
89+
if c == ")":
9090
t = []
91-
while stk[-1] != '(':
91+
while stk[-1] != "(":
9292
t.append(stk.pop())
9393
stk.pop()
9494
stk.extend(t)
9595
else:
9696
stk.append(c)
97-
return ''.join(stk)
97+
return "".join(stk)
9898
```
9999

100100
#### Java
101101

102102
```java
103103
class Solution {
104104
public String reverseParentheses(String s) {
105-
int n = s.length();
106-
int[] d = new int[n];
107-
Deque<Integer> stk = new ArrayDeque<>();
108-
for (int i = 0; i < n; ++i) {
109-
if (s.charAt(i) == '(') {
110-
stk.push(i);
111-
} else if (s.charAt(i) == ')') {
112-
int j = stk.pop();
113-
d[i] = j;
114-
d[j] = i;
115-
}
116-
}
117-
StringBuilder ans = new StringBuilder();
118-
int i = 0, x = 1;
119-
while (i < n) {
120-
if (s.charAt(i) == '(' || s.charAt(i) == ')') {
121-
i = d[i];
122-
x = -x;
105+
StringBuilder stk = new StringBuilder();
106+
for (char c : s.toCharArray()) {
107+
if (c == ')') {
108+
StringBuilder t = new StringBuilder();
109+
while (stk.charAt(stk.length() - 1) != '(') {
110+
t.append(stk.charAt(stk.length() - 1));
111+
stk.deleteCharAt(stk.length() - 1);
112+
}
113+
stk.deleteCharAt(stk.length() - 1);
114+
stk.append(t);
123115
} else {
124-
ans.append(s.charAt(i));
116+
stk.append(c);
125117
}
126-
i += x;
127118
}
128-
return ans.toString();
119+
return stk.toString();
129120
}
130121
}
131122
```
@@ -177,6 +168,27 @@ func reverseParentheses(s string) string {
177168
}
178169
```
179170

171+
#### TypeScript
172+
173+
```ts
174+
function reverseParentheses(s: string): string {
175+
const stk: string[] = [];
176+
for (const c of s) {
177+
if (c === ')') {
178+
const t: string[] = [];
179+
while (stk.at(-1)! !== '(') {
180+
t.push(stk.pop()!);
181+
}
182+
stk.pop();
183+
stk.push(...t);
184+
} else {
185+
stk.push(c);
186+
}
187+
}
188+
return stk.join('');
189+
}
190+
```
191+
180192
#### JavaScript
181193

182194
```js
@@ -185,68 +197,23 @@ func reverseParentheses(s string) string {
185197
* @return {string}
186198
*/
187199
var reverseParentheses = function (s) {
188-
const n = s.length;
189-
const d = new Array(n).fill(0);
190200
const stk = [];
191-
for (let i = 0; i < n; ++i) {
192-
if (s[i] == '(') {
193-
stk.push(i);
194-
} else if (s[i] == ')') {
195-
const j = stk.pop();
196-
d[i] = j;
197-
d[j] = i;
198-
}
199-
}
200-
let i = 0;
201-
let x = 1;
202-
const ans = [];
203-
while (i < n) {
204-
const c = s.charAt(i);
205-
if (c == '(' || c == ')') {
206-
i = d[i];
207-
x = -x;
201+
for (const c of s) {
202+
if (c === ')') {
203+
const t = [];
204+
while (stk.at(-1) !== '(') {
205+
t.push(stk.pop());
206+
}
207+
stk.pop();
208+
stk.push(...t);
208209
} else {
209-
ans.push(c);
210+
stk.push(c);
210211
}
211-
i += x;
212212
}
213-
return ans.join('');
213+
return stk.join('');
214214
};
215215
```
216216

217-
#### TypeScript
218-
219-
```ts
220-
function reverseParentheses(s: string): string {
221-
const n = s.length;
222-
const d = new Array(n).fill(0);
223-
const stk: number[] = [];
224-
for (let i = 0; i < n; ++i) {
225-
if (s[i] === '(') {
226-
stk.push(i);
227-
} else if (s[i] === ')') {
228-
const j = stk.pop()!;
229-
d[i] = j;
230-
d[j] = i;
231-
}
232-
}
233-
let i = 0;
234-
let x = 1;
235-
const ans: string[] = [];
236-
while (i < n) {
237-
const c = s.charAt(i);
238-
if (c === '(' || c === ')') {
239-
i = d[i];
240-
x = -x;
241-
} else {
242-
ans.push(c);
243-
}
244-
i += x;
245-
}
246-
return ans.join('');
247-
}
248-
```
249-
250217
<!-- tabs:end -->
251218

252219
<!-- solution:end -->
@@ -261,7 +228,7 @@ function reverseParentheses(s: string): string {
261228

262229
然后,我们从左到右遍历字符串,遇到 `(` 或者 `)` 时,根据 $d$ 数组跳到对应的位置,然后反转方向,继续遍历,直到遍历完整个字符串。
263230

264-
时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。
231+
时间复杂度 $O(n)$,空间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。
265232

266233
<!-- tabs:start -->
267234

@@ -274,21 +241,54 @@ class Solution:
274241
d = [0] * n
275242
stk = []
276243
for i, c in enumerate(s):
277-
if c == '(':
244+
if c == "(":
278245
stk.append(i)
279-
elif c == ')':
246+
elif c == ")":
280247
j = stk.pop()
281248
d[i], d[j] = j, i
282249
i, x = 0, 1
283250
ans = []
284251
while i < n:
285-
if s[i] in '()':
252+
if s[i] in "()":
286253
i = d[i]
287254
x = -x
288255
else:
289256
ans.append(s[i])
290257
i += x
291-
return ''.join(ans)
258+
return "".join(ans)
259+
```
260+
261+
#### Java
262+
263+
```java
264+
class Solution {
265+
public String reverseParentheses(String s) {
266+
int n = s.length();
267+
int[] d = new int[n];
268+
Deque<Integer> stk = new ArrayDeque<>();
269+
for (int i = 0; i < n; ++i) {
270+
if (s.charAt(i) == '(') {
271+
stk.push(i);
272+
} else if (s.charAt(i) == ')') {
273+
int j = stk.pop();
274+
d[i] = j;
275+
d[j] = i;
276+
}
277+
}
278+
StringBuilder ans = new StringBuilder();
279+
int i = 0, x = 1;
280+
while (i < n) {
281+
if (s.charAt(i) == '(' || s.charAt(i) == ')') {
282+
i = d[i];
283+
x = -x;
284+
} else {
285+
ans.append(s.charAt(i));
286+
}
287+
i += x;
288+
}
289+
return ans.toString();
290+
}
291+
}
292292
```
293293

294294
#### C++
@@ -357,80 +357,74 @@ func reverseParentheses(s string) string {
357357
}
358358
```
359359

360-
#### JavaScript
360+
#### TypeScript
361361

362-
```js
363-
function reverseParentheses(s) {
364-
const res = [];
362+
```ts
363+
function reverseParentheses(s: string): string {
365364
const n = s.length;
366-
const d = Array(n).fill(-1);
367-
const stk = [];
368-
369-
for (let i = 0; i < n; i++) {
370-
if (s[i] === '(') stk.push(i);
371-
else if (s[i] === ')') {
372-
const j = stk.pop();
365+
const d: number[] = Array(n).fill(0);
366+
const stk: number[] = [];
367+
for (let i = 0; i < n; ++i) {
368+
if (s[i] === '(') {
369+
stk.push(i);
370+
} else if (s[i] === ')') {
371+
const j = stk.pop()!;
373372
d[i] = j;
374373
d[j] = i;
375374
}
376375
}
377-
378-
for (let i = 0, forward = true; i < n; ) {
379-
const ch = s[i];
380-
381-
switch (s[i]) {
382-
case '(':
383-
case ')':
384-
i = forward ? d[i] - 1 : d[i] + 1;
385-
forward = !forward;
386-
break;
387-
388-
default:
389-
res.push(ch);
390-
i = forward ? i + 1 : i - 1;
376+
let i = 0;
377+
let x = 1;
378+
const ans: string[] = [];
379+
while (i < n) {
380+
const c = s.charAt(i);
381+
if ('()'.includes(c)) {
382+
i = d[i];
383+
x = -x;
384+
} else {
385+
ans.push(c);
391386
}
387+
i += x;
392388
}
393-
394-
return res.join('');
389+
return ans.join('');
395390
}
396391
```
397392

398-
#### TypeScript
393+
#### JavaScript
399394

400-
```ts
401-
function reverseParentheses(s: string): string {
402-
const res: string[] = [];
395+
```js
396+
/**
397+
* @param {string} s
398+
* @return {string}
399+
*/
400+
var reverseParentheses = function (s) {
403401
const n = s.length;
404-
const d = Array(n).fill(-1);
405-
const stk: number[] = [];
406-
407-
for (let i = 0; i < n; i++) {
408-
if (s[i] === '(') stk.push(i);
409-
else if (s[i] === ')') {
410-
const j = stk.pop()!;
402+
const d = Array(n).fill(0);
403+
const stk = [];
404+
for (let i = 0; i < n; ++i) {
405+
if (s[i] === '(') {
406+
stk.push(i);
407+
} else if (s[i] === ')') {
408+
const j = stk.pop();
411409
d[i] = j;
412410
d[j] = i;
413411
}
414412
}
415-
416-
for (let i = 0, forward = true; i < n; ) {
417-
const ch = s[i];
418-
419-
switch (s[i]) {
420-
case '(':
421-
case ')':
422-
i = forward ? d[i] - 1 : d[i] + 1;
423-
forward = !forward;
424-
break;
425-
426-
default:
427-
res.push(ch);
428-
i = forward ? i + 1 : i - 1;
413+
let i = 0;
414+
let x = 1;
415+
const ans = [];
416+
while (i < n) {
417+
const c = s.charAt(i);
418+
if ('()'.includes(c)) {
419+
i = d[i];
420+
x = -x;
421+
} else {
422+
ans.push(c);
429423
}
424+
i += x;
430425
}
431-
432-
return res.join('');
433-
}
426+
return ans.join('');
427+
};
434428
```
435429

436430
<!-- tabs:end -->

0 commit comments

Comments
 (0)