diff --git a/leetcode/1101-1200/1190.Reverse-Substrings-Between-Each-Pair-of-Parentheses/README.md b/leetcode/1101-1200/1190.Reverse-Substrings-Between-Each-Pair-of-Parentheses/README.md index b58cd9741..1ae7dc123 100644 --- a/leetcode/1101-1200/1190.Reverse-Substrings-Between-Each-Pair-of-Parentheses/README.md +++ b/leetcode/1101-1200/1190.Reverse-Substrings-Between-Each-Pair-of-Parentheses/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/1101-1200/1190.Reverse-Substrings-Between-Each-Pair-of-Parentheses/Solution.go b/leetcode/1101-1200/1190.Reverse-Substrings-Between-Each-Pair-of-Parentheses/Solution.go index d115ccf5e..33c409735 100644 --- a/leetcode/1101-1200/1190.Reverse-Substrings-Between-Each-Pair-of-Parentheses/Solution.go +++ b/leetcode/1101-1200/1190.Reverse-Substrings-Between-Each-Pair-of-Parentheses/Solution.go @@ -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() } diff --git a/leetcode/1101-1200/1190.Reverse-Substrings-Between-Each-Pair-of-Parentheses/Solution_test.go b/leetcode/1101-1200/1190.Reverse-Substrings-Between-Each-Pair-of-Parentheses/Solution_test.go index 14ff50eb4..96dcd82b7 100644 --- a/leetcode/1101-1200/1190.Reverse-Substrings-Between-Each-Pair-of-Parentheses/Solution_test.go +++ b/leetcode/1101-1200/1190.Reverse-Substrings-Between-Each-Pair-of-Parentheses/Solution_test.go @@ -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"}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }