Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit d46a362

Browse files
aQuaaQua
aQua
authored and
aQua
committed
344 ut pass
1 parent 52c5abb commit d46a362

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# [344. Reverse String](https://leetcode.com/problems/reverse-string/)
2+
3+
## 题目
4+
5+
Write a function that takes a string as input and returns the string reversed.
6+
7+
Example:
8+
Given s = "hello", return "olleh".
9+
10+
## 解题思路
11+
12+
见程序注释
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package Problem0344
2+
3+
func reverseString(s string) string {
4+
bytes := []byte(s)
5+
size := len(s)
6+
res := make([]byte, size)
7+
8+
for i := range bytes {
9+
res[i] = bytes[size-i-1]
10+
}
11+
12+
return string(res)
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package Problem0344
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
// tcs is testcase slice
11+
var tcs = []struct {
12+
s string
13+
ans string
14+
}{
15+
16+
{"hello", "olleh"},
17+
{"world", "dlrow"},
18+
19+
// 可以有多个 testcase
20+
}
21+
22+
func Test_reverseString(t *testing.T) {
23+
ast := assert.New(t)
24+
25+
for _, tc := range tcs {
26+
fmt.Printf("~~%v~~\n", tc)
27+
ast.Equal(tc.ans, reverseString(tc.s), "输入:%v", tc)
28+
}
29+
}
30+
31+
func Benchmark_reverseString(b *testing.B) {
32+
for i := 0; i < b.N; i++ {
33+
for _, tc := range tcs {
34+
reverseString(tc.s)
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)