【leetcode】1030. Matrix Cells in Distance Order
题目如下:
We are given a matrix with
R
rows andC
columns has cells with integer coordinates(r, c)
, where0 <= r < R
and0 <= 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 <= 100
1 <= C <= 100
0 <= r0 < R
0 <= 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 ...
随机推荐
- qbzt day4 下午
有向图的强连通分量 强联通:两个点之间可以互相到达 如果某个图任意两个点都是强联通的,那么称这个图强联通 如果一个图的子图是强联通的,那么称这个图是强联通子图 一个图的极大强联通子图被称作强连通分量 ...
- 主流架构 : MVP
1 背景 MVC 平时开发APP时会发现,activity职责非常重.以MVC角度来看: M:model数据操作层(网络请求,耗时操作,数据存取,其他逻辑操作) V:view,指xml布局文件,其实并 ...
- dataframe指定位置插入行
1 loc( ) 函数可以定位行后,并直接赋值插入. 如下可见loc函数对直接改变原来行的值 df = pd.DataFrame({ '动物' : ['狗','猫','兔'], '数量' : [ 3, ...
- linux查找所有文件中某个字符串
查找目录下的所有文件中是否含有某个字符串 find .|xargs grep -ri "IBM" 查找目录下的所有文件中是否含有某个字符串,并且只打印出文件名 find .|xar ...
- 19c的 rac在oracle linux7.4上搭建总结
准备: 1,ASM磁盘空间最低要求OCR的磁盘占用需求有了明显增长.为了方便操作,设置如下:External: 1个卷x40GNormal: 3个卷x30GHight: 5个卷x25GFlex: 3个 ...
- PS把一张白色背景的图片设为透明
方法一: 1.双击图层缩略图上的小锁图标(注意,这里不要拖动小锁进行删除锁定),弹出“新建图层”,确定 2.右键左侧第四个功能菜单,选择魔棒工具 3.用魔棒工具在白色背景区域点击一下,选中白色区域背景 ...
- Postman—上个接口返回数据作为下个接口入参
//将数据解析成json格式 var data=JSON.parse(responseBody); //获取totalRentPrice值 var totalRentPrice=jsonData.da ...
- JS 数组的常用方法归纳之不改变原数组和其他
不改变原数组的方法 concat() 连接两个或多个数组,不改变现有数组,返回新数组,添加的是数组中的元素 join(",") 把数组中的所有元素放入一个字符串,通过‘,’分隔符进 ...
- levelDB SSTable-1
创建sstable文件 了解了sstable文件的存储格式,以及Data Block的组织,下面就可以分析如何创建sstable文件了.相关代码在table_builder.h/.cc以及block_ ...
- java_第一年_JavaWeb(14)
EL :EL表达式主要用于获取数据.执行运算.获取对象.调用java方法: 获取数据 语法:"${标识符}" 会调用pageContext.findAttribute方法,从pag ...