Skip to content

Commit a7907eb

Browse files
committed
add: leetcode 419 test
1 parent c217fa2 commit a7907eb

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
"unsafe"
7+
)
8+
9+
type question419 struct {
10+
para419
11+
ans419
12+
}
13+
14+
// para 是参数
15+
// one 代表第一个参数
16+
type para419 struct {
17+
one [][]byte
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans419 struct {
23+
one int
24+
}
25+
26+
func Test_Problem419(t *testing.T) {
27+
28+
qs := []question419{
29+
30+
{
31+
para419{[][]byte{{'X', '.', '.', 'X'}, {'.', '.', '.', 'X'}, {'.', '.', '.', 'X'}}},
32+
ans419{2},
33+
},
34+
35+
{
36+
para419{[][]byte{{'.'}}},
37+
ans419{0},
38+
},
39+
}
40+
41+
fmt.Printf("------------------------Leetcode Problem 419------------------------\n")
42+
43+
for _, q := range qs {
44+
_, p := q.ans419, q.para419
45+
fmt.Printf("【input】:%v 【output】:%v\n", bytesArrayToStringArray(p.one), countBattleships(p.one))
46+
}
47+
fmt.Printf("\n\n\n")
48+
49+
}
50+
51+
// 在运行go test时 为了更直观地显示[][]byte中的字符而非ASCII码数值
52+
// bytesArrayToStringArray converts [][]byte to []string
53+
func bytesArrayToStringArray(b [][]byte) []string {
54+
s := make([]string, len(b))
55+
for i := range b {
56+
s[i] = fmt.Sprintf("[%v]", *(*string)(unsafe.Pointer(&b[i])))
57+
}
58+
return s
59+
}

0 commit comments

Comments
 (0)