forked from wufenggirl/LeetCode-in-Golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom-flip-matrix.go
executable file
·54 lines (45 loc) · 1.02 KB
/
random-flip-matrix.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
package problem0519
import "math/rand"
// Solution object will be instantiated and called as such:
// obj := Constructor(n_rows, n_cols);
// param_1 := obj.Flip();
// obj.Reset();
type Solution struct {
rows, cols int
total int // 矩阵中剩余的 0 的个数
rec map[int]int
}
// Constructor 构建 Solution
func Constructor(rows, cols int) Solution {
r := make(map[int]int)
return Solution{
rows: rows,
cols: cols,
total: rows * cols,
rec: r,
}
}
// Flip 选择 rows * cols 矩阵中的某个 0 进行翻转
func (s *Solution) Flip() []int {
if s.total == 0 {
return nil
}
index := rand.Intn(s.total)
cand := index
if change, isFound := s.rec[index]; isFound {
cand = change
}
r, c := cand/s.cols, cand%s.cols
s.total--
if change, isFound := s.rec[s.total]; isFound {
s.rec[index] = change
} else {
s.rec[index] = s.total
}
return []int{r, c}
}
// Reset 把 rows * cols 中的元素全部变成 0
func (s *Solution) Reset() {
s.total = s.rows * s.cols
s.rec = make(map[int]int)
}