【LeetCode】1030. Matrix Cells in Distance Order 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/matrix-cells-in-distance-order/
题目描述
We are given a matrix with R rows and C columns has cells with integer coordinates (r, c)
, where 0 <= r < R
and 0 <= 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
题目大意
有一个R * C的表格,把里面的每个坐标按照和点(r0, c0)的曼哈顿距离排序。
解题方法
排序
我知道这个题肯定有找规律的解法,但是一看R和C的范围发现总的格子不会超过10000个,直接排序是可以接受的。所以算出每个格子到点(r0, c0)的曼哈顿距离排序即可。
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]]
"""
dis = []
for r in range(R):
for c in range(C):
dis.append((abs(r0 - r) + abs(c0 - c), [r, c]))
dis.sort()
return [x for d, x in dis]
日期
2019 年 8 月 31 日 —— 赶在月底做个题
【LeetCode】1030. Matrix Cells in Distance Order 解题报告(Python)的更多相关文章
- 【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】1030. Matrix Cells in Distance Order
题目如下: We are given a matrix with R rows and C columns has cells with integer coordinates (r, c), whe ...
- 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)的 ...
- [Swift]LeetCode1030. 距离顺序排列矩阵单元格 | Matrix Cells in Distance Order
We are given a matrix with R rows and C columns has cells with integer coordinates (r, c), where 0 & ...
- 【LeetCode】245. Shortest Word Distance III 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+暴力检索 日期 题目地址:https://lee ...
- 【LeetCode】244. Shortest Word Distance II 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典保存出现位置 日期 题目地址:https://le ...
- 【LeetCode】341. Flatten Nested List Iterator 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归+队列 栈 日期 题目地址:https://lee ...
- 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...
随机推荐
- Nginx pid文件找不到 nginx: [error] open() "/run/nginx/nginx.pid" failed (2: No such file or directory)
Nginx pid文件找不到 nginx: [error] open() "/run/nginx/nginx.pid" failed (2: No such file or dir ...
- go定义接口以及类怎么使用接口
go定义接口以及类怎么使用接口 多态是指代码可以根据类型的具体实现采取不同行为的能力.如果一个类型实现了某个接口,所有使用这个接口的地方,都可以支持这种类型的值. 接口是用来定义行为的类型.这些被定义 ...
- Rust 指定安装目录
集群home目录被管理员限制了存储空间,rust安装要100多M,默认安装home目录下,查了一圈,没找到rust指定安装目录的办法. 这里记录下解决办法: 在想要安装的目录执行 mkdir -p c ...
- C4.5决策树-为什么可以选用信息增益来选特征
要理解信息增益,首先要明白熵是什么,开始很不理解熵,其实本质来看熵是一个度量值,这个值的大小能够很好的解释一些问题. 从二分类问题来看,可以看到,信息熵越是小的,说明分类越是偏斜(明确),可以理解为信 ...
- Hadoop入门 运行环境搭建
模板虚拟机 目录 模板虚拟机 1 硬件 2 操作系统 3 IP地址和主机名称 vm windows10 Hadoop100服务器 远程访问工具 其他准备 克隆虚拟机 克隆 修改主机名/ip 安装jdk ...
- 日常Java 2021/11/17
应用程序转换成Applet 将图形化的Java应用程序(是指,使用AWT的应用程序和使用java程序启动器启动的程序)转换成嵌入在web页面里的applet是很简单的.下面是将应用程序转换成.Appl ...
- 日常Java 2021/10/24
Java ArrrayList ArrayList类是一个可以动态修改的数组,没有固定大小的限制,可以在任何时候添加或者删除元素 ArrayList类在java.util包中使用之前需要引用 E:泛型 ...
- A Child's History of England.3
So, Julius Caesar came sailing over to this Island of ours, with eighty vessels and twelve thousand ...
- A Child's History of England.19
The King was at first as blind and stubborn as kings usually have been whensoever [每当] they have bee ...
- 8. LINUX shell 环境变量
wc –l file 计算文件行数, wc -w file 计算文件中的单词数, wc -c file 计算文件中的字符数 查看文件内容: cat .more