【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 ...
随机推荐
- 嵌入式Linux之gdb配置和使用
背景: ARM Cortext-A53核+Linux 4.1.12,内核空间64位,用户态32位,gdb版本7.10.1 GDB编译: 1)手动下载gdb-7.10.1.tar.gz源码编译 ./co ...
- Gradel 多渠道打包 代码混淆
http://blog.csdn.net/ttccaaa/article/details/47687241 http://www.bubuko.com/infodetail-987756.html h ...
- 二十二、正则表达式中的“r”含义
'''r:Python中字符串前面加上 r 表示原生字符串(rawstring)不使用r,那么匹配时候需要4个反斜杠,正则需要转化一次,python解释器需要转化一次'''mm="c:\\a ...
- 自动化生成 Openstack 新项目开发框架
目录 目录 前言 环境 openstack-project-generator 前言 Openstack Developer 应该都知道, 开发一个 Openstack 的新项目并不是一个从 0 到 ...
- Week 7 - 714. Best Time to Buy and Sell Stock with Transaction Fee & 718. Maximum Length of Repeated Subarray
714. Best Time to Buy and Sell Stock with Transaction Fee - Medium Your are given an array of intege ...
- Raudus入门(1)
Raudus入门(1) (2013-08-09 14:38:17) 转载▼ 标签: it 分类: Delphi 基于delphi做web应用,有个Raudus,基于对ext js的封装,可以在delp ...
- 关于一段有趣代码引出的String创建对象的解释
通常来说,我们认为hashCode不相同就为不同的对象.就这样由一段代码引发了一场讨论,代码如下: @Test public void stringCompare() { String s1 = &q ...
- Git的资源地址
下载地址:https://git-scm.com/downloads 安装教程: https://baijiahao.baidu.com/s?id=1619087367741781687&wf ...
- selenium验证码处理之cookie登录
在实际测试中会经常见到登录操作需要验证码验证登录 常见验证有以下几种: 验证码登录 图片识别 图片滑块识别验证 4.简单验证码计算 针对上面的登录验证解决办法有以下几种: 1.让开发去掉验证码 ...
- Python文件读写基本操作
https://www.jianshu.com/p/eab35af27e1c 1.打开文件操作 方法一: f = open('/mypy/test.txt') print f # 输出:<ope ...