首页 > 其他分享 >LeetCode 519. Random Flip Matrix 哈希Map

LeetCode 519. Random Flip Matrix 哈希Map

时间:2023-07-14 21:14:34浏览次数:40  
标签:Map Matrix map int self Random pos start matrix

There is an m x n binary grid matrix with all the values set 0 initially. Design an algorithm to randomly pick an index (i, j) where matrix[i][j] == 0 and flips it to 1. All the indices (i, j) where matrix[i][j] == 0 should be equally likely to be returned.

Optimize your algorithm to minimize the number of calls made to the built-in random function of your language and optimize the time and space complexity.

Implement the Solution class:

  • Solution(int m, int n) Initializes the object with the size of the binary matrix m and n.
  • int[] flip() Returns a random index [i, j] of the matrix where matrix[i][j] == 0 and flips it to 1.
  • void reset() Resets all the values of the matrix to be 0.
点击查看代码
class Solution:

    def __init__(self, m: int, n: int):
        self.row = m
        self.col = n
        self.start = 0
        self.end = m * n
        self.pos_map = {}
        

    def flip(self) -> List[int]:
        idx = random.randrange(self.start, self.end)
        res = self.pos_map.get(idx, idx)
        self.pos_map[idx] = self.pos_map.get(self.start, self.start)
        self.start += 1
        return (res // self.col, res % self.col)


    def reset(self) -> None:
        self.start = 0
        self.pos_map = {}
        


# Your Solution object will be instantiated and called as such:
# obj = Solution(m, n)
# param_1 = obj.flip()
# obj.reset()

标签:Map,Matrix,map,int,self,Random,pos,start,matrix
From: https://www.cnblogs.com/xinyu04/p/17554979.html

相关文章

  • VUE接收后端传递的map,解析并遍历
    后端传递map结果,前端接收时需要进行参数转化,转为前端的map,Object.entries方法进行转化,遍历时使用forof进行遍历,图中的item取出来的时一个数组对象,如{a:1,b:2},map对象时这样,item遍历第一轮时取出来的格式是[a,1],所有使用item[0]就可以取出key值,item[1]就可以取出value值 ......
  • Math函数之Random随机数、Date日期
    publicstaticvoidmain(String[]args)throwsParseException{Datedate1=newDate();//nowDatedate2=newDate(0);//计算机元年Datedate3=newDate(Long.MAX_VALUE);//毫秒数Datedate4=newDate(Long.MIN_VALUE);......
  • HashMap里面有哪些方法会更改modCount
    modCount是 HashMap 类中的一个成员变量,用于记录 HashMap 结构发生变更(如插入、删除、扩容等操作)的次数。在 HashMap 中,有以下方法会更改modCount的值:1.put(K key, V value):插入一个新的键值对。2.putAll(Map<? extends K, ? extends V> m):将一个 Map 中的所......
  • 1分钟理解map reduce,其实它就在我们身边
    linux平台下有个ls指令,大家都很熟悉:①ls|grep2008  查询文件名包含2008的文件(这其实就是一个map,找到需要的数据)②ls|grep2008|wc-l计算上述指令查询文件个数(这其实就是一个reduce,对找到数据进行汇总聚合) 再来一个例子,关于SQL:select*fromdevice ①select*fro......
  • MyBatis返回resultType=Map的用法, 返回List<Map<String,String>>
    <selectid="statOnlineAndNotlineNumber"resultType="java.util.Map"parameterType="java.lang.String">SELECTonline_stateasstate,COUNT(online_state)asnumberFROMwl_rm_t_vehicle_state<iftest="operatorCode!=nu......
  • set去重、map
    Set去重原理Set是Java中的一个接口,它的实现类(如HashSet.TreeSet等)用于存储一组不重复的元素。Set的去重原理是基于元素的hashCode0)和equals)方法。当向Set添加元素时,首先会调用被添加元索对象的hashCode0)方法来获取其哈希码。Set会根据哈希码判断元素是否已经存在于集......
  • Linux /dev/mapper/ubuntu--vg-ubuntu--lv磁盘空间不足的问题
    1.查看磁盘空间df-h从结果可以看到,/dev/mapper/ubuntu--vg-ubuntu--lv使用率偏高。2.查看块设备挂载情况lsblkNAMEMAJ:MINRMSIZEROTYPEMOUNTPOINTsda8:00931.5G0disk├─sda18:101M0par......
  • Java TreeMap 介绍与使用
    介绍TreeMap是Java集合框架中的一个类,它实现了SortedMap接口,可以存储键值对,并按照键的自然顺序或者指定的比较器进行排序。TreeMap的底层是一棵红黑树,这是一种自平衡的二叉搜索树,可以保证在插入,删除,查找等操作中的时间复杂度为O(logn)。使用要使用TreeMap,我们需要导入......
  • Java Map 通过key过滤
    pom文件:<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>31.1-jre</version></dependency>代码:packagecom.example.core.utils.collections;importcom.google.common.......
  • MyBatis,mapper找不到方法
       项目目录如下,可以看到是接口映射xml文件的mybatis此时运行项目会出现找不到save方法 解决方法:确保接口与xml文件路径一致在xml文件上再建一级mapper,并将其移入其中可  ......