Skip to content

Commit 85111ba

Browse files
committed
feat: update ts/js solutions to lc problem: No.1190
1 parent 775cf7f commit 85111ba

File tree

5 files changed

+54
-54
lines changed

5 files changed

+54
-54
lines changed

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,13 @@ var reverseParentheses = function (s) {
214214
};
215215
```
216216

217-
#### JavaScript
217+
#### TypeScript
218218

219219
```ts
220220
function reverseParentheses(s: string): string {
221221
const n = s.length;
222222
const d = new Array(n).fill(0);
223-
const stk = [];
223+
const stk: number[] = [];
224224
for (let i = 0; i < n; ++i) {
225225
if (s[i] === '(') {
226226
stk.push(i);
@@ -232,7 +232,7 @@ function reverseParentheses(s: string): string {
232232
}
233233
let i = 0;
234234
let x = 1;
235-
const ans = [];
235+
const ans: string[] = [];
236236
while (i < n) {
237237
const c = s.charAt(i);
238238
if (c === '(' || c === ')') {
@@ -360,18 +360,18 @@ func reverseParentheses(s string) string {
360360
#### JavaScript
361361

362362
```js
363-
function reverseParentheses(s: string): string {
364-
const res: string[] = [];
363+
function reverseParentheses(s) {
364+
const res = [];
365365
const n = s.length;
366-
const pairs = Array(n).fill(-1);
367-
const stack: number[] = [];
366+
const d = Array(n).fill(-1);
367+
const stk = [];
368368

369369
for (let i = 0; i < n; i++) {
370-
if (s[i] === '(') stack.push(i);
370+
if (s[i] === '(') stk.push(i);
371371
else if (s[i] === ')') {
372-
const j = stack.pop()!;
373-
pairs[i] = j;
374-
pairs[j] = i;
372+
const j = stk.pop();
373+
d[i] = j;
374+
d[j] = i;
375375
}
376376
}
377377

@@ -381,7 +381,7 @@ function reverseParentheses(s: string): string {
381381
switch (s[i]) {
382382
case '(':
383383
case ')':
384-
i = forward ? pairs[i] - 1 : pairs[i] + 1;
384+
i = forward ? d[i] - 1 : d[i] + 1;
385385
forward = !forward;
386386
break;
387387

@@ -401,15 +401,15 @@ function reverseParentheses(s: string): string {
401401
function reverseParentheses(s: string): string {
402402
const res: string[] = [];
403403
const n = s.length;
404-
const pairs = Array(n).fill(-1);
405-
const stack: number[] = [];
404+
const d = Array(n).fill(-1);
405+
const stk: number[] = [];
406406

407407
for (let i = 0; i < n; i++) {
408-
if (s[i] === '(') stack.push(i);
408+
if (s[i] === '(') stk.push(i);
409409
else if (s[i] === ')') {
410-
const j = stack.pop()!;
411-
pairs[i] = j;
412-
pairs[j] = i;
410+
const j = stk.pop()!;
411+
d[i] = j;
412+
d[j] = i;
413413
}
414414
}
415415

@@ -419,7 +419,7 @@ function reverseParentheses(s: string): string {
419419
switch (s[i]) {
420420
case '(':
421421
case ')':
422-
i = forward ? pairs[i] - 1 : pairs[i] + 1;
422+
i = forward ? d[i] - 1 : d[i] + 1;
423423
forward = !forward;
424424
break;
425425

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,13 @@ var reverseParentheses = function (s) {
207207
};
208208
```
209209

210-
#### JavaScript
210+
#### TypeScript
211211

212212
```ts
213213
function reverseParentheses(s: string): string {
214214
const n = s.length;
215215
const d = new Array(n).fill(0);
216-
const stk = [];
216+
const stk: number[] = [];
217217
for (let i = 0; i < n; ++i) {
218218
if (s[i] === '(') {
219219
stk.push(i);
@@ -225,7 +225,7 @@ function reverseParentheses(s: string): string {
225225
}
226226
let i = 0;
227227
let x = 1;
228-
const ans = [];
228+
const ans: string[] = [];
229229
while (i < n) {
230230
const c = s.charAt(i);
231231
if (c === '(' || c === ')') {
@@ -353,18 +353,18 @@ func reverseParentheses(s string) string {
353353
#### JavaScript
354354

355355
```js
356-
function reverseParentheses(s: string): string {
357-
const res: string[] = [];
356+
function reverseParentheses(s) {
357+
const res = [];
358358
const n = s.length;
359-
const pairs = Array(n).fill(-1);
360-
const stack: number[] = [];
359+
const d = Array(n).fill(-1);
360+
const stk = [];
361361

362362
for (let i = 0; i < n; i++) {
363-
if (s[i] === '(') stack.push(i);
363+
if (s[i] === '(') stk.push(i);
364364
else if (s[i] === ')') {
365-
const j = stack.pop()!;
366-
pairs[i] = j;
367-
pairs[j] = i;
365+
const j = stk.pop();
366+
d[i] = j;
367+
d[j] = i;
368368
}
369369
}
370370

@@ -374,7 +374,7 @@ function reverseParentheses(s: string): string {
374374
switch (s[i]) {
375375
case '(':
376376
case ')':
377-
i = forward ? pairs[i] - 1 : pairs[i] + 1;
377+
i = forward ? d[i] - 1 : d[i] + 1;
378378
forward = !forward;
379379
break;
380380

@@ -394,15 +394,15 @@ function reverseParentheses(s: string): string {
394394
function reverseParentheses(s: string): string {
395395
const res: string[] = [];
396396
const n = s.length;
397-
const pairs = Array(n).fill(-1);
398-
const stack: number[] = [];
397+
const d = Array(n).fill(-1);
398+
const stk: number[] = [];
399399

400400
for (let i = 0; i < n; i++) {
401-
if (s[i] === '(') stack.push(i);
401+
if (s[i] === '(') stk.push(i);
402402
else if (s[i] === ')') {
403-
const j = stack.pop()!;
404-
pairs[i] = j;
405-
pairs[j] = i;
403+
const j = stk.pop()!;
404+
d[i] = j;
405+
d[j] = i;
406406
}
407407
}
408408

@@ -412,7 +412,7 @@ function reverseParentheses(s: string): string {
412412
switch (s[i]) {
413413
case '(':
414414
case ')':
415-
i = forward ? pairs[i] - 1 : pairs[i] + 1;
415+
i = forward ? d[i] - 1 : d[i] + 1;
416416
forward = !forward;
417417
break;
418418

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function reverseParentheses(s: string): string {
22
const n = s.length;
33
const d = new Array(n).fill(0);
4-
const stk = [];
4+
const stk: number[] = [];
55
for (let i = 0; i < n; ++i) {
66
if (s[i] === '(') {
77
stk.push(i);
@@ -13,7 +13,7 @@ function reverseParentheses(s: string): string {
1313
}
1414
let i = 0;
1515
let x = 1;
16-
const ans = [];
16+
const ans: string[] = [];
1717
while (i < n) {
1818
const c = s.charAt(i);
1919
if (c === '(' || c === ')') {

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
function reverseParentheses(s) {
22
const res = [];
33
const n = s.length;
4-
const pairs = Array(n).fill(-1);
5-
const stack = [];
4+
const d = Array(n).fill(-1);
5+
const stk = [];
66

77
for (let i = 0; i < n; i++) {
8-
if (s[i] === '(') stack.push(i);
8+
if (s[i] === '(') stk.push(i);
99
else if (s[i] === ')') {
10-
const j = stack.pop();
11-
pairs[i] = j;
12-
pairs[j] = i;
10+
const j = stk.pop();
11+
d[i] = j;
12+
d[j] = i;
1313
}
1414
}
1515

@@ -19,7 +19,7 @@ function reverseParentheses(s) {
1919
switch (s[i]) {
2020
case '(':
2121
case ')':
22-
i = forward ? pairs[i] - 1 : pairs[i] + 1;
22+
i = forward ? d[i] - 1 : d[i] + 1;
2323
forward = !forward;
2424
break;
2525

solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/Solution2.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
function reverseParentheses(s: string): string {
22
const res: string[] = [];
33
const n = s.length;
4-
const pairs = Array(n).fill(-1);
5-
const stack: number[] = [];
4+
const d = Array(n).fill(-1);
5+
const stk: number[] = [];
66

77
for (let i = 0; i < n; i++) {
8-
if (s[i] === '(') stack.push(i);
8+
if (s[i] === '(') stk.push(i);
99
else if (s[i] === ')') {
10-
const j = stack.pop()!;
11-
pairs[i] = j;
12-
pairs[j] = i;
10+
const j = stk.pop()!;
11+
d[i] = j;
12+
d[j] = i;
1313
}
1414
}
1515

@@ -19,7 +19,7 @@ function reverseParentheses(s: string): string {
1919
switch (s[i]) {
2020
case '(':
2121
case ')':
22-
i = forward ? pairs[i] - 1 : pairs[i] + 1;
22+
i = forward ? d[i] - 1 : d[i] + 1;
2323
forward = !forward;
2424
break;
2525

0 commit comments

Comments
 (0)