前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Leetcode 519. Random Flip Matrix

Leetcode 519. Random Flip Matrix

作者头像
Tyan
发布2021-09-06 15:38:00
3180
发布2021-09-06 15:38:00
举报
文章被收录于专栏:SnailTyanSnailTyan

文章作者:Tyan 博客:noahsnail.com | CSDN | 简书

1. Description

Random Flip Matrix
Random Flip Matrix

2. Solution

**解析:**Version 1,使用单一数字作为矩阵的坐标索引,利用字典来保存访问过的矩阵索引,如果随机得到的索引在字典中存在,则继续进行随机索引,直至找到一个未访问过的索引,如果所有索引都访问过,则返回空坐标。

  • Version 1
代码语言:javascript
复制
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()

Reference

  1. https://leetcode.com/problems/random-flip-matrix/
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-08-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. Description
  • 2. Solution
  • Reference
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档