@@ -5,30 +5,61 @@ import (
5
5
"testing"
6
6
)
7
7
8
- func TestTwoSum (t * testing.T ) {
9
- tests := [][]int {
10
- []int {3 , 2 , 4 },
11
- []int {3 , 2 , 4 },
12
- []int {0 , 8 , 7 , 3 , 3 , 4 , 2 },
13
- []int {0 , 1 },
14
- }
15
- targets := []int {
16
- 6 ,
17
- 5 ,
18
- 11 ,
19
- 1 ,
20
- }
21
- results := [][]int {
22
- []int {1 , 2 },
23
- []int {0 , 1 },
24
- []int {1 , 3 },
25
- []int {0 , 1 },
8
+ type question1 struct {
9
+ para1
10
+ ans1
11
+ }
12
+
13
+ // para 是参数
14
+ // one 代表第一个参数
15
+ type para1 struct {
16
+ nums []int
17
+ target int
18
+ }
19
+
20
+ // ans 是答案
21
+ // one 代表第一个答案
22
+ type ans1 struct {
23
+ one []int
24
+ }
25
+
26
+ func Test_Problem1 (t * testing.T ) {
27
+
28
+ qs := []question1 {
29
+
30
+ question1 {
31
+ para1 {[]int {3 , 2 , 4 }, 6 },
32
+ ans1 {[]int {1 , 2 }},
33
+ },
34
+
35
+ question1 {
36
+ para1 {[]int {3 , 2 , 4 }, 5 },
37
+ ans1 {[]int {0 , 1 }},
38
+ },
39
+
40
+ question1 {
41
+ para1 {[]int {0 , 8 , 7 , 3 , 3 , 4 , 2 }, 11 },
42
+ ans1 {[]int {1 , 3 }},
43
+ },
44
+
45
+ question1 {
46
+ para1 {[]int {0 , 1 }, 1 },
47
+ ans1 {[]int {0 , 1 }},
48
+ },
49
+
50
+ question1 {
51
+ para1 {[]int {0 , 3 }, 5 },
52
+ ans1 {[]int {}},
53
+ },
54
+
55
+ // 如需多个测试,可以复制上方元素。
26
56
}
57
+
27
58
fmt .Printf ("------------------------Leetcode Problem 1------------------------\n " )
28
- for i := 0 ; i < len (targets ); i ++ {
29
- fmt .Printf ("nums = %v target = %v result = %v\n " , tests [i ], targets [i ], twoSum (tests [i ], targets [i ]))
30
- if ret := twoSum (tests [i ], targets [i ]); ret [0 ] != results [i ][0 ] && ret [1 ] != results [i ][1 ] {
31
- t .Fatalf ("case %d fails: %v\n " , i , ret )
32
- }
59
+
60
+ for _ , q := range qs {
61
+ _ , p := q .ans1 , q .para1
62
+ fmt .Printf ("【input】:%v 【output】:%v\n " , p , twoSum (p .nums , p .target ))
33
63
}
64
+ fmt .Printf ("\n \n \n " )
34
65
}
0 commit comments