Python | Leetcode Python题解之第519题随机翻转矩阵
题目:
题解:
class Solution:def __init__(self, m: int, n: int):self.m = mself.n = nself.total = m * nself.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.nself.map.clear()