|
| 1 | +package leetcode |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +type question238 struct { |
| 9 | + para238 |
| 10 | + ans238 |
| 11 | +} |
| 12 | + |
| 13 | +// para is the parameter |
| 14 | +// one represents the first parameter |
| 15 | +type para238 struct { |
| 16 | + one []int |
| 17 | +} |
| 18 | + |
| 19 | +// ans is the answer |
| 20 | +// one represents the first answer |
| 21 | +type ans238 struct { |
| 22 | + one []int |
| 23 | +} |
| 24 | + |
| 25 | +func Test_Problem238(t *testing.T) { |
| 26 | + |
| 27 | + qs := []question238{ |
| 28 | + |
| 29 | + { |
| 30 | + para238{[]int{1, 2, 3, 4}}, |
| 31 | + ans238{[]int{24, 12, 8, 6}}, |
| 32 | + }, |
| 33 | + |
| 34 | + { |
| 35 | + para238{[]int{-1, 1, 0, -3, 3}}, |
| 36 | + ans238{[]int{0, 0, 9, 0, 0}}, |
| 37 | + }, |
| 38 | + |
| 39 | + { |
| 40 | + para238{[]int{2, 3}}, |
| 41 | + ans238{[]int{3, 2}}, |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + fmt.Printf("------------------------Leetcode Problem 238------------------------\n") |
| 46 | + |
| 47 | + for _, q := range qs { |
| 48 | + a, p := q.ans238, q.para238 |
| 49 | + got := productExceptSelf(p.one) |
| 50 | + fmt.Printf("【input】:%v 【output】:%v\n", p, got) |
| 51 | + if !equal238(got, a.one) { |
| 52 | + t.Fatalf("input %v: expected %v, got %v", p.one, a.one, got) |
| 53 | + } |
| 54 | + } |
| 55 | + fmt.Printf("\n\n\n") |
| 56 | +} |
| 57 | + |
| 58 | +func equal238(a, b []int) bool { |
| 59 | + if len(a) != len(b) { |
| 60 | + return false |
| 61 | + } |
| 62 | + for i := range a { |
| 63 | + if a[i] != b[i] { |
| 64 | + return false |
| 65 | + } |
| 66 | + } |
| 67 | + return true |
| 68 | +} |
0 commit comments