problem0519

package
v0.0.0-...-d3fc627 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 15, 2020 License: MIT Imports: 1 Imported by: 0

README

519. Random Flip Matrix

题目

You are given the number of rows n_rowsand number of columns n_colsof a2Dbinary matrixwhere all values are initially 0.Write a function flipwhich choosesa 0 valueuniformly 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 andspace complexity.

Note:

  1. 1 <= n_rows, n_cols<= 10000
  2. 0 <= row.id < n_rows and 0 <= col.id < n_cols
  3. flipwill not be called when the matrix has no0 values left.
  4. the total number of calls toflipand resetwill not exceed1000.

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 calledand theirarguments. Solution's constructorhas two arguments, n_rows and n_cols.flipand reset havenoarguments.Argumentsarealways wrapped with a list, even if there aren't any.

解题思路

见程序注释

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Solution

type Solution struct {
	// contains filtered or unexported fields
}

Solution object will be instantiated and called as such: obj := Constructor(n_rows, n_cols); param_1 := obj.Flip(); obj.Reset();

func Constructor

func Constructor(rows, cols int) Solution

Constructor 构建 Solution

func (*Solution) Flip

func (s *Solution) Flip() []int

Flip 选择 rows * cols 矩阵中的某个 0 进行翻转

func (*Solution) Reset

func (s *Solution) Reset()

Reset 把 rows * cols 中的元素全部变成 0

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL