【leetcode】1030. Matrix Cells in Distance Order
题目如下:
We are given a matrix with
Rrows andCcolumns has cells with integer coordinates(r, c), where0 <= r < Rand0 <= c < C.Additionally, we are given a cell in that matrix with coordinates
(r0, c0).Return the coordinates of all cells in the matrix, sorted by their distance from
(r0, c0)from smallest distance to largest distance. Here, the distance between two cells(r1, c1)and(r2, c2)is the Manhattan distance,|r1 - r2| + |c1 - c2|. (You may return the answer in any order that satisfies this condition.)Example 1:
Input: R = 1, C = 2, r0 = 0, c0 = 0
Output: [[0,0],[0,1]]
Explanation: The distances from (r0, c0) to other cells are: [0,1]Example 2:
Input: R = 2, C = 2, r0 = 0, c0 = 1
Output: [[0,1],[0,0],[1,1],[1,0]]
Explanation: The distances from (r0, c0) to other cells are: [0,1,1,2]
The answer [[0,1],[1,1],[0,0],[1,0]] would also be accepted as correct.Example 3:
Input: R = 2, C = 3, r0 = 1, c0 = 2
Output: [[1,2],[0,2],[1,1],[0,1],[1,0],[0,0]]
Explanation: The distances from (r0, c0) to other cells are: [0,1,1,2,2,3]
There are other answers that would also be accepted as correct, such as [[1,2],[1,1],[0,2],[1,0],[0,1],[0,0]].Note:
1 <= R <= 1001 <= C <= 1000 <= r0 < R0 <= c0 < C
解题思路:BFS,就这样。
代码如下:
class Solution(object):
def allCellsDistOrder(self, R, C, r0, c0):
"""
:type R: int
:type C: int
:type r0: int
:type c0: int
:rtype: List[List[int]]
"""
visit = []
for i in range(R):
visit.append([0] * C)
direction = [(1,0),(-1,0),(0,1),(0,-1)]
queue = [(r0,c0)]
res = []
while len(queue) > 0:
x,y = queue.pop(0)
if visit[x][y] == 1:
continue
res.append([x, y])
visit[x][y] = 1
for (i,j) in direction:
if x + i >= 0 and x + i < R and y + j >=0 and y+j < C and visit[x+i][y+j] == 0:
queue.append((x+i,y+j))
return res
【leetcode】1030. Matrix Cells in Distance Order的更多相关文章
- 【LeetCode】1030. Matrix Cells in Distance Order 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...
- 【Leetcode_easy】1030. Matrix Cells in Distance Order
problem 1030. Matrix Cells in Distance Order 参考 1. Leetcode_easy_1030. Matrix Cells in Distance Orde ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- Matrix Cells in Distance Order
Matrix Cells in Distance Order We are given a matrix with R rows and C columns has cells with intege ...
- LeetCode.1030-曼哈顿距离排序矩阵单元格(Matrix Cells in Distance Order)
这是小川的第384次更新,第412篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第246题(顺位题号是1030).我们给出一个矩阵,其中R行和C列具有整数坐标(r,c)的 ...
- 【LeetCode】Set Matrix Zeroes 解题报告
今天看到CSDN博客的勋章换了图表,同一时候也添加显示了博客等级,看起来都听清新的,感觉不错! [题目] Given a m x n matrix, if an element is 0, set i ...
- 【LeetCode】Spiral Matrix(螺旋矩阵)
这是LeetCode里的第54道题. 题目要求: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ...
- 【LeetCode】957. Prison Cells After N Days 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 周期是14 日期 题目地址:https://leet ...
- 【LeetCode】358. Rearrange String k Distance Apart 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/rearrang ...
随机推荐
- C++中让人忽视的左值和右值
前言 为了了解C++11的新特性右值引用,不得不重新认识一下左右值.学习之初,最快的理解,莫过于望文生义了,右值那就是赋值号右边的值,左值就是赋值号左边的值.在中学的数学的学习中,我们理解的是,左值等 ...
- Ueditor1.4.4 Jsp版本视频上传成功,重新编辑时无法打开、在文本框内无法显示、html源码显示src为空
1. 编辑 ueditor.config.js 第355行 将 whitList 改为 whiteList 2.编辑ueditor.all.js 注释掉7343.7344.7345行代码,即: var ...
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_01 File类_5_File类获取功能的方法
获取的方法 GetAbsolutepath 传递一个相对路径进去,查看输出的结果 输出的还是绝对的路径 getPath 获取的就是构造方法中传递的路径,可以传递绝对路径也可以传递相对路径 实际上toS ...
- javaweb学习总结(十)——HttpServletRequest对象(一) https://www.cnblogs.com/xdp-gacl/p/3798347.html
一.HttpServletRequest介绍 HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象 ...
- 分布式ID生成器 snowflake(雪花)算法
在springboot的启动类中引入 @Bean public IdWorker idWorkker(){ return new IdWorker(1, 1); } 在代码中调用 @Autowired ...
- Reinforcement Learning Index Page
Reinforcement Learning Posts Step-by-step from Markov Property to Markov Decision Process Markov Dec ...
- 20190922 On Java8 第二十一章 数组
第二十一章 数组 一等对象 对象数组和基元数组在使用上是完全相同的.唯一的不同之处就是对象数组存储的是对象的引用,而基元数组则直接存储基本数据类型的值. 多维数组 Array.deepToString ...
- vuer-cli 安装笔记
电脑上装 的软件全卸载了.需要 重装 .整理了一下vue-cli脚手架搭建 1 先下载git 2 再下载node 3安装淘宝镜像 (https://npm.taobao.org/) 4 安装webpa ...
- JMeter学习笔记16-如何输出HTML格式的性能测试报告
文本来学习下,如何输入HTML格式的JMeter测试报告.前面已经介绍, 如果要做性能测试,需要在GUI上设计好你的Test Plan,设置各种场景和负载值,包括多少个线程,多少个用户,循环多少次.设 ...
- 027 (H5*) react01
目录 正文 1:创建Vue项目 1: 全局安装 vue-cli cnpm install --global vue-cli 2: 创建一个基于 webpack 模板的新项目 vue init webp ...