文章作者:Tyan
博客:noahsnail.com ?|? CSDN ?|? 簡書
1. Description

Random Flip Matrix
2. Solution
解析:Version 1,使用單一數字作為矩陣的坐標索引,利用字典來保存訪問過的矩陣索引,如果隨機得到的索引在字典中存在,則繼續(xù)進行隨機索引,直至找到一個未訪問過的索引,如果所有索引都訪問過,則返回空坐標。
- Version 1
class Solution:
def __init__(self, m: int, n: int):
self.m = m
self.n = n
self.coordinates = m * n
self.visited = {}
def flip(self) -> List[int]:
if len(self.visited) == self.coordinates:
return []
index = random.randrange(self.coordinates)
while index in self.visited:
index = random.randrange(self.coordinates)
self.visited[index] = 1
return [index // self.n, index % self.n]
def reset(self) -> None:
self.visited = {}
# Your Solution object will be instantiated and called as such:
# obj = Solution(m, n)
# param_1 = obj.flip()
# obj.reset()