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

  1. 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 随机翻转矩阵的更多相关文章

  1. [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 ...

  2. [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 ...

  3. Java实现 LeetCode 756 金字塔转换矩阵(DFS)

    756. 金字塔转换矩阵 现在,我们用一些方块来堆砌一个金字塔. 每个方块用仅包含一个字母的字符串表示. 使用三元组表示金字塔的堆砌规则如下: 对于三元组(A, B, C) ,"C" ...

  4. Leetcode 542:01 矩阵 01

    Leetcode 542:01 矩阵 01 Matrix### 题目: 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . Given a matr ...

  5. 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 ...

  6. 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. ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 【MySQL基础总结】运算符的使用

    运算符的使用 算数运算符 比较运算符 结果只能为TRUE(1)或FALSE(0) 逻辑运算符 运算符的优先级 可以通过括号改变优先级 示例 算数运算符 比较运算符 逻辑运算符

  2. 【基础】excel如何根据数据内容显示不同颜色。

    需求: 店柜完成率排名相比上阶段升降,升显示绿色“↑“,降显示红色“↓”,持平显示黑色“-”. 步骤: 第一步 先计算两次排名的差值(本次排名-上次排名). 第二步 对差值列设置单元格格式,设置格式如 ...

  3. Java元注解@Retention规则

    @Retention是java当中的一个元注解,该元注解通常都是用于对软件的测试 1.适用方式:     @Retention(RetentionPolicy.RUNTIME)     @interf ...

  4. Mysql 常用函数(10)- strcmp 函数

    Mysql常用函数的汇总,可看下面系列文章 https://www.cnblogs.com/poloyy/category/1765164.html strcmp 的作用 比较两个字符串的顺序是否完全 ...

  5. 给出两个单词(start和end)与一个字典,找出从start到end的最短转换序列

    问题 给出两个单词(start和end)与一个字典,找出从start到end的最短转换序列.规则如下: 一次只能改变一个字母 中间单词必须在字典里存在 例如: 给出 start = "hit ...

  6. 终于,帮开发写了一个bug

    写在文章的开头 最近项目比较紧,尤其前端的的需求比较多,作为一名测试,也会些vue,本着加快项目进度的美好想法,就自告奋勇的向组长承接了一部分开发的任务,其中有个需求需要在我们的广告管理后台新增一个上 ...

  7. spark机器学习从0到1介绍入门之(一)

      一.什么是机器学习 机器学习(Machine Learning, ML)是一门多领域交叉学科,涉及概率论.统计学.逼近论.凸分析.算法复杂度理论等多门学科.专门研究计算机怎样模拟或实现人类的学习行 ...

  8. LaunchScreen作为启动图设置,修改无效的解决方案

    原有的推流APP用launchScreen做的启动图,现在要修改一张,发现修改无效. 当前测试的方法有 1,重启Xcode  卸载app 清楚xcode缓存 2,修改launchScreen.stor ...

  9. 测试工程中引入Masonry记录

    测试工程中需要引入Masonry,在进行添加新库时发现了几个问题,记录如下,方便有相同问题的朋友查找解决:   1,podfile中添加 pod ‘Masonry’ 后,pod install --v ...

  10. 51Nod - 1050

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1050 解题思路: It's so beautiful ! 循环 ...