作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/random-flip-matrix/description/ 题目描述: 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…
题目如下: 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: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题目地址:https://leetcode.com/problems/spiral-matrix/description/ 题目描述 Given a matrix of m x n elements (m rows, n columns), return all elements of the mat…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先构建数组再遍历实现翻转 日期 题目地址:https://leetcode.com/problems/transpose-matrix/description/ 题目描述 Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipp…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:两两比较 方法二:切片相等 方法三:判断每条对角线 日期 题目地址:https://leetcode.com/problems/toeplitz-matrix/description/ 题目描述 A matrix is Toeplitz if every diagonal from top-left to bottom-right has t…
题目要求 In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data. You're given a matrix represented by a two-dimensional array, and two positive integers r and …
1. 问题 给定一个全零矩阵的行和列,实现flip函数随机把一个0变成1并返回索引,实现rest函数将所有数归零. 2. 思路 拒绝采样 (1)先计算矩阵的元素个数(行乘以列),记作n,那么[0, n-1]相当于矩阵下标对应的一维索引. (2)用一个arrays数组存放矩阵元素为1的索引.每次从 [0, n-1]取数,这个数可以表示矩阵元素的索引,如果取的数已经在这个数组里,说明这个索引对应的矩阵元素已经被flip为1了,则放弃,继续取数,直到取到的数不在数组里(对应的矩阵元素为0),就把取到的…
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode-cn.com/problems/flip-game/ 题目描述 You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + a…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-paths/description/ 题目描述: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either…
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer…