Skip to content

Add solution and test-cases for problem 1190 #931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
# [1190.Reverse Substrings Between Each Pair of Parentheses][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
You are given a string `s` that consists of lower case English letters and brackets.

Reverse the strings in each pair of matching parentheses, starting from the innermost one.

Your result should **not** contain any brackets.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: s = "(abcd)"
Output: "dcba"
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Reverse Substrings Between Each Pair of Parentheses
```go
```
Input: s = "(u(love)i)"
Output: "iloveu"
Explanation: The substring "love" is reversed first, then the whole string is reversed.
```

**Example 3:**

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

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
package Solution

func Solution(x bool) bool {
return x
import (
"bytes"
)

func Solution(s string) string {
stack := make([][]byte, 0)
buf := bytes.NewBuffer([]byte{})
for idx := 0; idx < len(s); idx++ {
if s[idx] == '(' {
dst := make([]byte, len(buf.Bytes()))
copy(dst, buf.Bytes())

stack = append(stack, dst)
buf.Reset()
continue
}
if s[idx] == ')' {
tmp := buf.Bytes()
tmpl := len(tmp)
bs := make([]byte, tmpl)
buf.Reset()
for i := tmpl - 1; i >= 0; i-- {
bs[tmpl-i-1] = tmp[i]
}

l := len(stack) - 1
top := stack[l]
stack = stack[:l]

top = append(top, bs...)
buf.Write(top)
continue
}
buf.WriteByte(s[idx])
}

return buf.String()
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs string
expect string
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "(u(love)i)", "iloveu"},
{"TestCase2", "(abcd)", "dcba"},
{"TestCase3", "(ed(et(oc))el)", "leetcode"},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading