[Leetcode] 01 Matrix
问题: https://leetcode.com/problems/01-matrix/#/description
基本思路:广度优先遍历,根据所有最短距离为N的格找到所有距离为N+1的格,直到所有的格都找到了最短距离。
具体步骤:
首先初始化矩阵,值为0的格其最短距离也是0,保持不变;值为1的格是后面需要计算最短距离的格,我们需要一个整数来标识它们,这里可以选择一个负数或者整数的最大值。
然后进行若干轮计算,第N轮会根据已经计算出的最短距离为N-1的所有格,来找出所有最短距离为N的格。
例如,第1轮计算就是根据所有最短距离为0的格,找到所有距离为1的格。
第5轮计算,就是根据所有最短距离为4的格,找到所有距离为5的格。
具体怎么找呢?对每一个最短距离为N-1的格,跟它相邻的上下左右四个方向的格,其中还没有计算出最短距离的格(即值为负数或者整数的最大值的)的最短距离一定是N。
如果不是N而是小于N的某个数M的话,那么这个格在前面的M-1轮就应该找到了,跟第M-1轮找到所有的最短距离为M的格矛盾。
用Java 8实现如下:
public class Solution {
public int[][] updateMatrix(int[][] matrix) {
Queue<int[]> queue = new LinkedList<>();
// 初始化矩阵
for(int i = 0; i < matrix.length; i++) {
for(int j = 0; j < matrix[0].length; j++) {
if(matrix[i][j] == 0) {
queue.offer(new int[]{i, j});
}else {
matrix[i][j] = Integer.MAX_VALUE;
}
}
}
int[][] dirs = new int[][]{{0, -1},{-1, 0},{0, 1},{1, 0}};
while(!queue.isEmpty()) {
int[] cell = queue.poll();
for(int i = 0; i < dirs.length; i++) {
int r = cell[0] + dirs[i][0];
int c = cell[1] + dirs[i][1];
if(r >= 0 && r < matrix.length && c >= 0 && c < matrix[0].length && matrix[cell[0]][cell[1]] + 1 < matrix[r][c]) {
matrix[r][c] = matrix[cell[0]][cell[1]] + 1;
queue.offer(new int[]{r, c});
}
}
}
return matrix;
}
}
[Leetcode] 01 Matrix的更多相关文章
- [LeetCode] 01 Matrix 零一矩阵
Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell. The distance b ...
- [LeetCode] 01 Matrix 题解
题意 # 思路 我一开始的时候想的是嘴 # 实现 ```cpp // // include "../PreLoad.h" class Solution { public: /** ...
- [Leetcode Week10]01 Matrix
01 Matrix 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/01-matrix/description/ Description Given a ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- [leetcode] 542. 01 Matrix (Medium)
给予一个矩阵,矩阵有1有0,计算每一个1到0需要走几步,只能走上下左右. 解法一: 利用dp,从左上角遍历一遍,再从右下角遍历一遍,dp存储当前位置到0的最短距离. 十分粗心的搞错了col和row,改 ...
- leetcode 542. 01 Matrix 、663. Walls and Gates(lintcode) 、773. Sliding Puzzle 、803. Shortest Distance from All Buildings
542. 01 Matrix https://www.cnblogs.com/grandyang/p/6602288.html 将所有的1置为INT_MAX,然后用所有的0去更新原本位置为1的值. 最 ...
- LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题
Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...
- 计算机学院大学生程序设计竞赛(2015’12)01 Matrix
01 Matrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- hdu 01 Matrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission ...
随机推荐
- winform窗体最小化
const int WM_SYSCOMMAND = 0x112;const int SC_CLOSE = 0xF060;const int SC_MINIMIZE = 0xF020;const int ...
- THINKphp中复杂的查询
字符串拼接查询 案例一:拼接字符串(多条件查询) $where = ''; //定义字符串,用于拼接满足条件的数据字段 $value = []; // 定义空数组,用于接收值 if(!empty($n ...
- PHP 函数漏洞总结
1.MD5 compare漏洞 PHP在处理哈希字符串时,会利用"!="或"=="来对哈希值进行比较,它把每一个以"0E"开头的哈希值都解释 ...
- linux系统ubuntu18.04安装mysql(5.7)
本文是参考mysql官网整理而成,顺便把一些遇到的问题记载下来. ①将MySQLAPT存储库添加到系统的软件存储库列表中 ---->下载APT存储库(下载链接) ---->安装APT存 ...
- MyBatis学习日记(二): MyBatis Say Hello
首先在Eclipse中创建一个maven工程: 在maven工程下的pom.xml文件中添加MyBatis.MySQL.Junit依赖: <project xmlns="http:// ...
- Dora.Interception,为.NET Core度身打造的AOP框架 [1]:更加简练的编程体验
很久之前开发了一个名为Dora.Interception的开源AOP框架(github地址:https://github.com/jiangjinnan/Dora,如果你觉得这个这框架还有那么一点价值 ...
- html简单的知识
分布式版本控制git pwd查询当前目录 ls ls -la git config --global user.name xxx git config --global user. ...
- C语言之各个位数上的数值之和
#include<stdio.h> #include<stdlib.h> void main() { int num; ; int x,y; printf("请输入一 ...
- ubuntu下安装mongodb
https://www.cnblogs.com/shileima/p/7823434.html
- vi设置行号
首先,我们先打开一个文件,用vim 文件名 就可以直接使用vim打开 我们事先写了一些内容在这个txt里面,我们可以看到如下内容 在这里,我们可以直接敲命令, :set number 或者 ...