Skip to content

Commit 1553afa

Browse files
committed
[E:57/533, M:57/974, H:6/387] add No: 151 Reverse Words in a String
1 parent aad7953 commit 1553afa

File tree

9 files changed

+393
-1
lines changed

9 files changed

+393
-1
lines changed

questions/serial/中等/151/README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
## [翻转字符串里的单词](https://leetcode-cn.com/problems/reverse-words-in-a-string/)
2+
3+
给定一个字符串,逐个翻转字符串中的每个单词。
4+
5+
**说明:**
6+
7+
* 无空格字符构成一个 **单词**
8+
* 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。
9+
* 如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。
10+
11+
 
12+
13+
**示例 1:**
14+
15+
`**输入:**"`the sky is blue`"
16+
**输出:**"`blue is sky the`"
17+
`
18+
19+
**示例 2:**
20+
21+
`**输入:**"  hello world!  "
22+
**输出:**"world! hello"
23+
**解释:**输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。
24+
`
25+
26+
**示例 3:**
27+
28+
`**输入:**"a good   example"
29+
**输出:**"example good a"
30+
**解释:**如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。
31+
`
32+
33+
**示例 4:**
34+
35+
`**输入:**s = " Bob Loves Alice "
36+
**输出:**"Alice Loves Bob"
37+
`
38+
39+
**示例 5:**
40+
41+
`**输入:**s = "Alice does not even like bob"
42+
**输出:**"bob like even not does Alice"
43+
`
44+
45+
 
46+
47+
**提示:**
48+
49+
* `1 <= s.length <= 10
50+
4
51+
`
52+
* `s` 包含英文大小写字母、数字和空格 `' '`
53+
* `s`**至少存在一个** 单词
54+
55+
 
56+
57+
**进阶:**
58+
59+
* 请尝试使用 _O_(1) 额外空间复杂度的原地解法。
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"reflect"
7+
"time"
8+
9+
"github.com/gladmo/leetcode/leet"
10+
"github.com/gladmo/leetcode/questions/serial/中等/151/golang/solution"
11+
)
12+
13+
func main() {
14+
/*
15+
16+
"the sky is blue"
17+
18+
*/
19+
20+
tests := []struct {
21+
name string
22+
input string
23+
want string
24+
}{
25+
{
26+
name: "test-the sky is blue",
27+
input: "the sky is blue",
28+
want: "blue is sky the",
29+
},
30+
{
31+
name: "test- hello world! ",
32+
input: " hello world! ",
33+
want: "world! hello",
34+
},
35+
{
36+
name: "test-a good example",
37+
input: "a good example",
38+
want: "example good a",
39+
},
40+
{
41+
name: "test- Bob Loves Alice ",
42+
input: " Bob Loves Alice ",
43+
want: "Alice Loves Bob",
44+
},
45+
{
46+
name: "test-Alice does not even like bob",
47+
input: "Alice does not even like bob",
48+
want: "bob like even not does Alice",
49+
},
50+
}
51+
52+
testLog := leet.NewTestLog(len(tests))
53+
defer testLog.Render()
54+
55+
timeoutDuration := time.Second * 2
56+
57+
for idx, test := range tests {
58+
// 超时检测
59+
got := test.want
60+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
61+
got = solution.Export(test.input)
62+
cancel()
63+
})
64+
65+
if timeout {
66+
testLog.Fail(idx+1, test.name, "timeout")
67+
continue
68+
}
69+
70+
if !reflect.DeepEqual(test.want, got) {
71+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
72+
continue
73+
}
74+
75+
testLog.Pass(idx+1, test.name)
76+
}
77+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package solution
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"runtime/debug"
7+
)
8+
9+
func Export(s string) string {
10+
defer func() {
11+
if r := recover(); r != nil {
12+
fmt.Println("Params: ", s)
13+
fmt.Println("Panic:", r)
14+
fmt.Println()
15+
debug.PrintStack()
16+
os.Exit(0)
17+
}
18+
}()
19+
20+
return reverseWords(s)
21+
}
22+
23+
/****************************************************/
24+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
25+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
26+
/****************************************************/
27+
28+
func reverseWords(s string) string {
29+
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package solution
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"runtime/debug"
7+
)
8+
9+
func Export(s string) string {
10+
defer func() {
11+
if r := recover(); r != nil {
12+
fmt.Println("Params: ", s)
13+
fmt.Println("Panic:", r)
14+
fmt.Println()
15+
debug.PrintStack()
16+
os.Exit(0)
17+
}
18+
}()
19+
20+
return reverseWords(s)
21+
}
22+
23+
/****************************************************/
24+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
25+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
26+
/****************************************************/
27+
28+
func reverseWords(s string) string {
29+
30+
}

questions/store.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
## [翻转字符串里的单词](https://leetcode-cn.com/problems/reverse-words-in-a-string/)
2+
3+
给定一个字符串,逐个翻转字符串中的每个单词。
4+
5+
**说明:**
6+
7+
* 无空格字符构成一个 **单词**
8+
* 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。
9+
* 如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。
10+
11+
 
12+
13+
**示例 1:**
14+
15+
`**输入:**"`the sky is blue`"
16+
**输出:**"`blue is sky the`"
17+
`
18+
19+
**示例 2:**
20+
21+
`**输入:**"  hello world!  "
22+
**输出:**"world! hello"
23+
**解释:**输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。
24+
`
25+
26+
**示例 3:**
27+
28+
`**输入:**"a good   example"
29+
**输出:**"example good a"
30+
**解释:**如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。
31+
`
32+
33+
**示例 4:**
34+
35+
`**输入:**s = " Bob Loves Alice "
36+
**输出:**"Alice Loves Bob"
37+
`
38+
39+
**示例 5:**
40+
41+
`**输入:**s = "Alice does not even like bob"
42+
**输出:**"bob like even not does Alice"
43+
`
44+
45+
 
46+
47+
**提示:**
48+
49+
* `1 <= s.length <= 10
50+
4
51+
`
52+
* `s` 包含英文大小写字母、数字和空格 `' '`
53+
* `s`**至少存在一个** 单词
54+
55+
 
56+
57+
**进阶:**
58+
59+
* 请尝试使用 _O_(1) 额外空间复杂度的原地解法。
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"reflect"
7+
"time"
8+
9+
"github.com/gladmo/leetcode/leet"
10+
"github.com/gladmo/leetcode/questions/serial/中等/151/golang/solution"
11+
)
12+
13+
func main() {
14+
/*
15+
16+
"the sky is blue"
17+
18+
*/
19+
20+
tests := []struct {
21+
name string
22+
input string
23+
want string
24+
}{
25+
{
26+
name: "test-the sky is blue",
27+
input: "the sky is blue",
28+
want: "blue is sky the",
29+
},
30+
{
31+
name: "test- hello world! ",
32+
input: " hello world! ",
33+
want: "world! hello",
34+
},
35+
{
36+
name: "test-a good example",
37+
input: "a good example",
38+
want: "example good a",
39+
},
40+
{
41+
name: "test- Bob Loves Alice ",
42+
input: " Bob Loves Alice ",
43+
want: "Alice Loves Bob",
44+
},
45+
{
46+
name: "test-Alice does not even like bob",
47+
input: "Alice does not even like bob",
48+
want: "bob like even not does Alice",
49+
},
50+
}
51+
52+
testLog := leet.NewTestLog(len(tests))
53+
defer testLog.Render()
54+
55+
timeoutDuration := time.Second * 2
56+
57+
for idx, test := range tests {
58+
// 超时检测
59+
got := test.want
60+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
61+
got = solution.Export(test.input)
62+
cancel()
63+
})
64+
65+
if timeout {
66+
testLog.Fail(idx+1, test.name, "timeout")
67+
continue
68+
}
69+
70+
if !reflect.DeepEqual(test.want, got) {
71+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
72+
continue
73+
}
74+
75+
testLog.Pass(idx+1, test.name)
76+
}
77+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package solution
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"runtime/debug"
7+
)
8+
9+
func Export(s string) string {
10+
defer func() {
11+
if r := recover(); r != nil {
12+
fmt.Println("Params: ", s)
13+
fmt.Println("Panic:", r)
14+
fmt.Println()
15+
debug.PrintStack()
16+
os.Exit(0)
17+
}
18+
}()
19+
20+
return reverseWords(s)
21+
}
22+
23+
/****************************************************/
24+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
25+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
26+
/****************************************************/
27+
28+
func reverseWords(s string) string {
29+
30+
}

0 commit comments

Comments
 (0)