-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathinterview_01.08_test.go
111 lines (99 loc) · 1.42 KB
/
interview_01.08_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//编写一种算法,若M × N矩阵中某个元素为0,则将其所在的行与列清零。
//
//
//
// 示例 1:
//
// 输入:
//[
// [1,1,1],
// [1,0,1],
// [1,1,1]
//]
//输出:
//[
// [1,0,1],
// [0,0,0],
// [1,0,1]
//]
//
//
// 示例 2:
//
// 输入:
//[
// [0,1,2,0],
// [3,4,5,2],
// [1,3,1,5]
//]
//输出:
//[
// [0,0,0,0],
// [0,4,5,0],
// [0,3,1,0]
//]
//
// Related Topics 数组
// 👍 30 👎 0
package leetcode
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestSetZeroes(t *testing.T) {
matrix := [][]int{
{0, 1, 2, 0},
{3, 4, 5, 2},
{1, 3, 1, 5},
}
setZeroes(matrix)
assert.Equal(t, [][]int{
{0, 0, 0, 0},
{0, 4, 5, 0},
{0, 3, 1, 0},
}, matrix)
matrix = [][]int{
{1, 1, 1},
{1, 0, 1},
{1, 1, 1},
}
setZeroes(matrix)
assert.Equal(t, [][]int{
{1, 0, 1},
{0, 0, 0},
{1, 0, 1},
}, matrix)
}
func setZeroes(matrix [][]int) {
var (
n = len(matrix)
m = len(matrix[0])
list = make([][]int, n)
)
copy(list, matrix)
if m == 0 || n == 0 {
return
}
var xs = make(map[int]struct{})
for y := 0; y < n; y++ {
ixs := make(map[int]struct{})
for x := 0; x < m; x++ {
if matrix[y][x] == 0 {
ixs[x] = struct{}{}
}
}
if len(ixs) == 0 {
continue
}
for k := range ixs {
xs[k] = struct{}{}
}
list[y] = make([]int, m)
}
for x := range xs {
for y := 0; y < n; y++ {
list[y][x] = 0
}
}
copy(matrix, list)
}