Skip to content

Commit b6811c9

Browse files
committed
Add solution and test-cases for problem 557
1 parent e5972ba commit b6811c9

File tree

4 files changed

+38
-22
lines changed

4 files changed

+38
-22
lines changed

leetcode/501-600/0543.Diameter-of-Binary-Tree/Solution_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type SolutionFuncType func(*TreeNode) int
1515

1616
var SolutionFuncList = []SolutionFuncType{
1717
diameterOfBinaryTree_1,
18-
diameterOfBinaryTree_2,
18+
//diameterOfBinaryTree_2,
1919
}
2020

2121
// Test case info struct

leetcode/501-600/0557.Reverse-Words-in-a-String-III/README.md

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
11
# [557.Reverse Words in a String III][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+
Given a string `s`, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
75

86
**Example 1:**
97

108
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
9+
Input: s = "Let's take LeetCode contest"
10+
Output: "s'teL ekat edoCteeL tsetnoc"
1311
```
1412

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

20-
### 思路1
21-
> ...
22-
Reverse Words in a String III
23-
```go
2415
```
25-
16+
Input: s = "God Ding"
17+
Output: "doG gniD"
18+
```
2619

2720
## 结语
2821

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

3-
func Solution(x bool) bool {
4-
return x
3+
import "bytes"
4+
5+
func Solution(s string) string {
6+
buf := bytes.NewBufferString("")
7+
stack := make([]string, 0)
8+
for idx := len(s) - 1; idx >= 0; idx-- {
9+
if s[idx] == ' ' {
10+
stack = append(stack, buf.String())
11+
buf.Reset()
12+
continue
13+
}
14+
buf.WriteByte(s[idx])
15+
}
16+
stack = append(stack, buf.String())
17+
buf.Reset()
18+
first := true
19+
for idx := len(stack) - 1; idx >= 0; idx-- {
20+
writeObj := stack[idx]
21+
if !first {
22+
writeObj = " " + writeObj
23+
}
24+
first = false
25+
buf.WriteString(writeObj)
26+
}
27+
return buf.String()
528
}

leetcode/501-600/0557.Reverse-Words-in-a-String-III/Solution_test.go

Lines changed: 5 additions & 5 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", "Let's take LeetCode contest", "s'teL ekat edoCteeL tsetnoc"},
17+
{"TestCase2", "God Ding", "doG gniD"},
18+
{"TestCase3", "abc", "cba"},
1919
}
2020

2121
// 开始测试

0 commit comments

Comments
 (0)