Skip to content

Commit cee13c4

Browse files
committed
[E:63/533, M:57/974, H:6/387] add No: 557 Reverse Words in a String III
1 parent f3dd783 commit cee13c4

File tree

9 files changed

+271
-1
lines changed

9 files changed

+271
-1
lines changed

questions/serial/简单/557/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## [反转字符串中的单词 III](https://leetcode-cn.com/problems/reverse-words-in-a-string-iii/)
2+
3+
给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。
4+
5+
 
6+
7+
**示例:**
8+
9+
`**输入:**"Let's take LeetCode contest"
10+
**输出:**"s'teL ekat edoCteeL tsetnoc"
11+
`
12+
13+
 
14+
15+
**
16+
17+
18+
提示:**
19+
20+
21+
22+
23+
* 在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格。
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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/简单/557/golang/solution"
11+
)
12+
13+
func main() {
14+
15+
tests := []struct {
16+
name string
17+
input string
18+
want string
19+
}{
20+
{
21+
name: "test-Let's take LeetCode contest",
22+
input: "Let's take LeetCode contest",
23+
want: "s'teL ekat edoCteeL tsetnoc",
24+
},
25+
}
26+
27+
testLog := leet.NewTestLog(len(tests))
28+
defer testLog.Render()
29+
30+
timeoutDuration := time.Second * 2
31+
32+
for idx, test := range tests {
33+
// 超时检测
34+
got := test.want
35+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
36+
got = solution.Export(test.input)
37+
cancel()
38+
})
39+
40+
if timeout {
41+
testLog.Fail(idx+1, test.name, "timeout")
42+
continue
43+
}
44+
45+
if !reflect.DeepEqual(test.want, got) {
46+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
47+
continue
48+
}
49+
50+
testLog.Pass(idx+1, test.name)
51+
}
52+
}
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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## [反转字符串中的单词 III](https://leetcode-cn.com/problems/reverse-words-in-a-string-iii/)
2+
3+
给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。
4+
5+
 
6+
7+
**示例:**
8+
9+
`**输入:**"Let's take LeetCode contest"
10+
**输出:**"s'teL ekat edoCteeL tsetnoc"
11+
`
12+
13+
 
14+
15+
**
16+
17+
18+
提示:**
19+
20+
21+
22+
23+
* 在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格。
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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/简单/557/golang/solution"
11+
)
12+
13+
func main() {
14+
15+
tests := []struct {
16+
name string
17+
input string
18+
want string
19+
}{
20+
{
21+
name: "test-Let's take LeetCode contest",
22+
input: "Let's take LeetCode contest",
23+
want: "s'teL ekat edoCteeL tsetnoc",
24+
},
25+
}
26+
27+
testLog := leet.NewTestLog(len(tests))
28+
defer testLog.Render()
29+
30+
timeoutDuration := time.Second * 2
31+
32+
for idx, test := range tests {
33+
// 超时检测
34+
got := test.want
35+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
36+
got = solution.Export(test.input)
37+
cancel()
38+
})
39+
40+
if timeout {
41+
testLog.Fail(idx+1, test.name, "timeout")
42+
continue
43+
}
44+
45+
if !reflect.DeepEqual(test.want, got) {
46+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
47+
continue
48+
}
49+
50+
testLog.Pass(idx+1, test.name)
51+
}
52+
}
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+
}
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)