Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ec4cae3

Browse files
rain84yanglbme
andauthored
feat: add solutions to lc problem: No.1190 (doocs#3260)
Co-authored-by: Libin YANG <contact@yanglibin.info>
1 parent bc563da commit ec4cae3

File tree

10 files changed

+450
-161
lines changed

10 files changed

+450
-161
lines changed

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

Lines changed: 156 additions & 53 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,32 +197,20 @@ 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

@@ -228,7 +228,7 @@ var reverseParentheses = function (s) {
228228

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

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

233233
<!-- tabs:start -->
234234

@@ -241,21 +241,54 @@ class Solution:
241241
d = [0] * n
242242
stk = []
243243
for i, c in enumerate(s):
244-
if c == '(':
244+
if c == "(":
245245
stk.append(i)
246-
elif c == ')':
246+
elif c == ")":
247247
j = stk.pop()
248248
d[i], d[j] = j, i
249249
i, x = 0, 1
250250
ans = []
251251
while i < n:
252-
if s[i] in '()':
252+
if s[i] in "()":
253253
i = d[i]
254254
x = -x
255255
else:
256256
ans.append(s[i])
257257
i += x
258-
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+
}
259292
```
260293

261294
#### C++
@@ -324,6 +357,76 @@ func reverseParentheses(s string) string {
324357
}
325358
```
326359

360+
#### TypeScript
361+
362+
```ts
363+
function reverseParentheses(s: string): string {
364+
const n = s.length;
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()!;
372+
d[i] = j;
373+
d[j] = i;
374+
}
375+
}
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);
386+
}
387+
i += x;
388+
}
389+
return ans.join('');
390+
}
391+
```
392+
393+
#### JavaScript
394+
395+
```js
396+
/**
397+
* @param {string} s
398+
* @return {string}
399+
*/
400+
var reverseParentheses = function (s) {
401+
const n = s.length;
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();
409+
d[i] = j;
410+
d[j] = i;
411+
}
412+
}
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);
423+
}
424+
i += x;
425+
}
426+
return ans.join('');
427+
};
428+
```
429+
327430
<!-- tabs:end -->
328431

329432
<!-- solution:end -->

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /