[Swift]LeetCode519. 随机翻转矩阵 | Random Flip Matrix
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10397430.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
You are given the number of rows n_rows
and number of columns n_cols
of a 2D binary matrix where all values are initially 0. Write a function flip
which chooses a 0 value uniformly at random, changes it to 1, and then returns the position [row.id, col.id]
of that value. Also, write a function reset
which sets all values back to 0. Try to minimize the number of calls to system's Math.random() and optimize the time and space complexity.
Note:
1 <= n_rows, n_cols <= 10000
0 <= row.id < n_rows
and0 <= col.id < n_cols
flip
will not be called when the matrix has no 0 values left.- the total number of calls to
flip
andreset
will not exceed 1000.
Example 1:
Input:
["Solution","flip","flip","flip","flip"]
[[2,3],[],[],[],[]]
Output: [null,[0,1],[1,2],[1,0],[1,1]]
Example 2:
Input:
["Solution","flip","flip","reset","flip"]
[[1,2],[],[],[],[]]
Output: [null,[0,0],[0,1],null,[0,0]]
Explanation of Input Syntax:
The input is two lists: the subroutines called and their arguments. Solution
's constructor has two arguments, n_rows
and n_cols
. flip
and reset
have no arguments. Arguments are always wrapped with a list, even if there aren't any.
题中给出一个 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
2. 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
没有参数,参数总会以列表形式给出,哪怕该列表为空
1 class Solution { 2 var row:Int 3 var col:Int 4 var size:Int 5 var m:[Int:Int] 6 7 init(_ n_rows: Int, _ n_cols: Int) { 8 row = n_rows 9 col = n_cols 10 size = row * col 11 m = [Int:Int]() 12 } 13 14 func flip() -> [Int] { 15 var id:Int = Int.random(in:0..<size) 16 var val:Int = id 17 size -= 1 18 if m[id] != nil {id = m[id]!} 19 m[val] = m[size] != nil ? m[size] : size 20 return [id / col, id % col] 21 } 22 23 func reset() { 24 m = [Int:Int]() 25 size = row * col 26 } 27 } 28 29 /** 30 * Your Solution object will be instantiated and called as such: 31 * let obj = Solution(n_rows, n_cols) 32 * let ret_1: [Int] = obj.flip() 33 * obj.reset() 34 */ 35