Skip to content

Commit 3af63bb

Browse files
committed
add 151 problem solution
1 parent 62dc111 commit 3af63bb

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# [1. Add Sum][title]
2+
3+
## Description
4+
5+
Given an input string, reverse the string word by word.
6+
7+
**Example 1:**
8+
9+
```
10+
Input: "the sky is blue"
11+
Output: "blue is sky the"
12+
```
13+
14+
**Example 2:**
15+
16+
```
17+
Input: " hello world! "
18+
Output: "world! hello"
19+
Explanation: Your reversed string should not contain leading or trailing spaces.
20+
```
21+
22+
**Example 2:**
23+
24+
```
25+
Input: "a good example"
26+
Output: "example good a"
27+
Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.
28+
```
29+
**Tags:** Math, String
30+
31+
32+
33+
## 题意
34+
> 给定一个字符串,请将字符串中单词的顺序倒过来。
35+
> 注意:
36+
37+
- 单词是指连续非空格字符;
38+
- 单词之间可能包含多余空格,请在结果中删除多余空格,使得单词之间仅包含一个空格;
39+
- 请删除字符串首尾多余的空格;
40+
41+
## 题解
42+
43+
### 思路1
44+
> 。。。。
45+
46+
```go
47+
func reverseWords(s string) string {
48+
// 用自带的函数拆分数组
49+
words := strings.Fields(s)
50+
51+
i := 0
52+
j := len(words) - 1
53+
54+
for i < j {
55+
// 左右22交换
56+
words[i], words[j] = words[j], words[i]
57+
i++
58+
j--
59+
}
60+
61+
return strings.Join(words, " ")
62+
}
63+
64+
```
65+
66+
### 思路2
67+
> 思路2
68+
```go
69+
70+
```
71+
72+
## 结语
73+
74+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-leetcode][me]
75+
76+
[title]: https://leetcode.com/problems/two-sum/description/
77+
[me]: https://github.com/kylesliu/awesome-golang-leetcode
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package Solution
2+
3+
import (
4+
"strings"
5+
)
6+
7+
func reverseWords(s string) string {
8+
// 用自带的函数拆分数组
9+
words := strings.Fields(s)
10+
11+
i := 0
12+
j := len(words) - 1
13+
14+
for i < j {
15+
// 左右22交换
16+
words[i], words[j] = words[j], words[i]
17+
i++
18+
j--
19+
}
20+
21+
return strings.Join(words, " ")
22+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package Solution
2+
3+
import (
4+
"reflect"
5+
"strconv"
6+
"testing"
7+
)
8+
9+
func TestSolution(t *testing.T) {
10+
// 测试用例
11+
cases := []struct {
12+
name string
13+
inputs string
14+
expect string
15+
}{
16+
{"TestCase", "the sky is blue", "blue is sky the"},
17+
{"TestCase", " hello world! ", "world! hello"},
18+
{"TestCase", "a good example", "example good a"},
19+
{"TestCase", "eht yks si eulb", "blue is sky the"},
20+
}
21+
22+
// 开始测试
23+
for i, c := range cases {
24+
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
25+
got := reverseWords(c.inputs)
26+
if !reflect.DeepEqual(got, c.expect) {
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
28+
c.expect, got, c.inputs)
29+
}
30+
})
31+
}
32+
}
33+
34+
// 压力测试
35+
func BenchmarkSolution(b *testing.B) {
36+
37+
}
38+
39+
// 使用案列
40+
func ExampleSolution() {
41+
42+
}

0 commit comments

Comments
 (0)