1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
random-flip-matrix.py 845 Bytes
一键复制 编辑 原始数据 按行查看 历史
Allen Liu 提交于 7年前 . add complexity
# Time: ctor: O(1)
# flip: O(1)
# reset: O(min(f, r * c))
# Space: O(min(f, r * c))
import random
class Solution(object):
def __init__(self, n_rows, n_cols):
"""
:type n_rows: int
:type n_cols: int
"""
self.__n_rows = n_rows
self.__n_cols = n_cols
self.__n = n_rows*n_cols
self.__lookup = {}
def flip(self):
"""
:rtype: List[int]
"""
self.__n -= 1
target = random.randint(0, self.__n)
x = self.__lookup.get(target, target)
self.__lookup[target] = self.__lookup.get(self.__n, self.__n)
return divmod(x, self.__n_cols)
def reset(self):
"""
:rtype: void
"""
self.__n = self.__n_rows*self.__n_cols
self.__lookup = {}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助