Python | Leetcode Python题解之第519题随机翻转矩阵
题目:
题解:
class Solution:
def __init__(self, m: int, n: int):
self.m = m
self.n = n
self.total = m * n
self.map = {}
def flip(self) -> List[int]:
x = random.randint(0, self.total - 1)
self.total -= 1
# 查找位置 x 对应的映射
idx = self.map.get(x, x)
# 将位置 x 对应的映射设置为位置 total 对应的映射
self.map[x] = self.map.get(self.total, self.total)
return [idx // self.n, idx % self.n]
def reset(self) -> None:
self.total = self.m * self.n
self.map.clear()