mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
add: leetcode 0519 solution
This commit is contained in:
43
leetcode/0519.Random-Flip-Matrix/519.Random Flip Matrix.go
Normal file
43
leetcode/0519.Random-Flip-Matrix/519.Random Flip Matrix.go
Normal file
@ -0,0 +1,43 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
type Solution struct {
|
||||
r int
|
||||
c int
|
||||
total int
|
||||
mp map[int]int
|
||||
}
|
||||
|
||||
func Constructor(m int, n int) Solution {
|
||||
return Solution{
|
||||
r: m,
|
||||
c: n,
|
||||
total: m * n,
|
||||
mp: map[int]int{},
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Solution) Flip() []int {
|
||||
k := rand.Intn(this.total)
|
||||
val := k
|
||||
if v, ok := this.mp[k]; ok {
|
||||
val = v
|
||||
}
|
||||
if _, ok := this.mp[this.total-1]; ok {
|
||||
this.mp[k] = this.mp[this.total-1]
|
||||
} else {
|
||||
this.mp[k] = this.total - 1
|
||||
}
|
||||
delete(this.mp, this.total-1)
|
||||
this.total--
|
||||
newR, newC := val/this.c, val%this.c
|
||||
return []int{newR, newC}
|
||||
}
|
||||
|
||||
func (this *Solution) Reset() {
|
||||
this.total = this.r * this.c
|
||||
this.mp = map[int]int{}
|
||||
}
|
Reference in New Issue
Block a user