Skip to content

Commit 03d93c9

Browse files
authored
Merge pull request #931 from 0xff-dev/1190
Add solution and test-cases for problem 1190
2 parents f5e30de + 6ea8dab commit 03d93c9

File tree

3 files changed

+63
-22
lines changed

3 files changed

+63
-22
lines changed

leetcode/1101-1200/1190.Reverse-Substrings-Between-Each-Pair-of-Parentheses/README.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
# [1190.Reverse Substrings Between Each Pair of Parentheses][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given a string `s` that consists of lower case English letters and brackets.
5+
6+
Reverse the strings in each pair of matching parentheses, starting from the innermost one.
7+
8+
Your result should **not** contain any brackets.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: s = "(abcd)"
14+
Output: "dcba"
1315
```
1416

15-
## 题意
16-
> ...
17-
18-
## 题解
17+
**Example 2:**
1918

20-
### 思路1
21-
> ...
22-
Reverse Substrings Between Each Pair of Parentheses
23-
```go
2419
```
20+
Input: s = "(u(love)i)"
21+
Output: "iloveu"
22+
Explanation: The substring "love" is reversed first, then the whole string is reversed.
23+
```
24+
25+
**Example 3:**
2526

27+
```
28+
Input: s = "(ed(et(oc))el)"
29+
Output: "leetcode"
30+
Explanation: First, we reverse the substring "oc", then "etco", and finally, the whole string.
31+
```
2632

2733
## 结语
2834

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,40 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import (
4+
"bytes"
5+
)
6+
7+
func Solution(s string) string {
8+
stack := make([][]byte, 0)
9+
buf := bytes.NewBuffer([]byte{})
10+
for idx := 0; idx < len(s); idx++ {
11+
if s[idx] == '(' {
12+
dst := make([]byte, len(buf.Bytes()))
13+
copy(dst, buf.Bytes())
14+
15+
stack = append(stack, dst)
16+
buf.Reset()
17+
continue
18+
}
19+
if s[idx] == ')' {
20+
tmp := buf.Bytes()
21+
tmpl := len(tmp)
22+
bs := make([]byte, tmpl)
23+
buf.Reset()
24+
for i := tmpl - 1; i >= 0; i-- {
25+
bs[tmpl-i-1] = tmp[i]
26+
}
27+
28+
l := len(stack) - 1
29+
top := stack[l]
30+
stack = stack[:l]
31+
32+
top = append(top, bs...)
33+
buf.Write(top)
34+
continue
35+
}
36+
buf.WriteByte(s[idx])
37+
}
38+
39+
return buf.String()
540
}

leetcode/1101-1200/1190.Reverse-Substrings-Between-Each-Pair-of-Parentheses/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs string
14+
expect string
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "(u(love)i)", "iloveu"},
17+
{"TestCase2", "(abcd)", "dcba"},
18+
{"TestCase3", "(ed(et(oc))el)", "leetcode"},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)