Java实现 LeetCode 519 随机翻转矩阵
519. 随机翻转矩阵
题中给出一个 n 行 n 列的二维矩阵 (n_rows,n_cols),且所有值被初始化为 0。要求编写一个 flip 函数,均匀随机的将矩阵中的 0 变为 1,并返回该值的位置下标 [row_id,col_id];同样编写一个 reset 函数,将所有的值都重新置为 0。尽量最少调用随机函数 Math.random(),并且优化时间和空间复杂度。
注意:
1.1 <= n_rows, n_cols <= 10000
- 0 <= row.id < n_rows 并且 0 <= col.id < n_cols
3.当矩阵中没有值为 0 时,不可以调用 flip 函数
4.调用 flip 和 reset 函数的次数加起来不会超过 1000 次
示例 1:
输入:
[“Solution”,“flip”,“flip”,“flip”,“flip”]
[[2,3],[],[],[],[]]
输出: [null,[0,1],[1,2],[1,0],[1,1]]
示例 2:
输入:
[“Solution”,“flip”,“flip”,“reset”,“flip”]
[[1,2],[],[],[],[]]
输出: [null,[0,0],[0,1],null,[0,0]]
输入语法解释:
输入包含两个列表:被调用的子程序和他们的参数。Solution 的构造函数有两个参数,分别为 n_rows 和 n_cols。flip 和 reset 没有参数,参数总会以列表形式给出,哪怕该列表为空
PS:
自己映射一个数组
class Solution {
Map<Integer, Integer> V = new HashMap<>();
int nr, nc, rem;
Random rand = new Random();
public Solution(int n_rows, int n_cols) {
nr = n_rows;
nc = n_cols;
rem = nr * nc;
}
public int[] flip() {
int r = rand.nextInt(rem--);
int x = V.getOrDefault(r, r);
V.put(r, V.getOrDefault(rem, rem));
return new int[]{x / nc, x % nc};
}
public void reset() {
V.clear();
rem = nr * nc;
}
}
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(n_rows, n_cols);
* int[] param_1 = obj.flip();
* obj.reset();
*/
Java实现 LeetCode 519 随机翻转矩阵的更多相关文章
- [LeetCode] Random Flip Matrix 随机翻转矩阵
You are given the number of rows n_rows and number of columns n_cols of a 2D binary matrix where all ...
- [Swift]LeetCode519. 随机翻转矩阵 | Random Flip Matrix
You are given the number of rows n_rows and number of columns n_cols of a 2D binary matrix where all ...
- Java实现 LeetCode 756 金字塔转换矩阵(DFS)
756. 金字塔转换矩阵 现在,我们用一些方块来堆砌一个金字塔. 每个方块用仅包含一个字母的字符串表示. 使用三元组表示金字塔的堆砌规则如下: 对于三元组(A, B, C) ,"C" ...
- Leetcode 542:01 矩阵 01
Leetcode 542:01 矩阵 01 Matrix### 题目: 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . Given a matr ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
随机推荐
- Vulnhb 靶场系列:Jarbas1.0
靶场镜像 官网 信息收集 攻击机kali IP地址 通过nmap 进行主机发现,发现目标机IP地址 nmap -sP 192.168.227.1/24 参数说明: -sP (Ping扫描) 该选项告诉 ...
- mybatis分页助手分页
一.编写dao及配置文件 (1)配置文件两种方式 第一种: <!--SqlSeesionFactoryBean对象--><bean id="sqlSessionFactor ...
- jQuery中prevAll得到的DOM元素顺序问题
学习笔记,记录下学习中遇到的问题. 使用jQuery中的prevAll可以查找当前元素之前所有的同辈元素,但是却存在一个问题:得到的同辈元素的为正常顺序的反方向. 举个例子: <!doctype ...
- 推荐一款复式记账软件——GnuCash
本文需要搞清楚两个事情,第一,什么是复式记账:第二,GnuCash操作 复式记账,来自百度百科的解释:复式记账法是以资产与权益平衡关系作为记账基础,对于每一笔经济业务,都要以相等的金额在两个或两个以上 ...
- SSH_ProductCRUD的项目结构与配置文件
项目结构 各类配置文件 <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hib ...
- UPD链接实现稳健传输案例
使用的类 DatagramSocket 用于发送数据和接收数据 此类的构造方法: DatagramSocket(); DatagramSocket(端口号); ...
- C#命名空间大全
Microsoft.Jscript Jscript语言进行编译和代码生成的Jscript运行库和类. Microsoft.VisualBasic Visual Basic .NET运行库.此运行库与V ...
- 来自AI的Tips——情景智能
来自AI的Tips--情景智能 上一次我们介绍了华为快服务智慧平台是什么,今天我们来侃一侃平台最有代表性的一个流量入口--情景智能(AI Tips). 首先情景智能在哪呢?大家可以拿出自己的华 ...
- 使用OS模块来获取文件路径
1.os模块概述 Python os模块包含普遍的操作系统功能.如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的. 2.常用方法 os.getcwd() 函数得到当前工作目录,即当前Pyth ...
- 配置单机Kafka
配置单机kafka 关闭selinux,开启防火墙9092端口 1.关闭selinux vi /etc/selinux/config #SELINUX=enforcing #注释掉 #SELINUXT ...