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()
原文地址:https://blog.csdn.net/Mopes__/article/details/143353940
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!