问题: 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的更多相关文章

  1. [LeetCode] 01 Matrix 零一矩阵

    Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell. The distance b ...

  2. [LeetCode] 01 Matrix 题解

    题意 # 思路 我一开始的时候想的是嘴 # 实现 ```cpp // // include "../PreLoad.h" class Solution { public: /** ...

  3. [Leetcode Week10]01 Matrix

    01 Matrix 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/01-matrix/description/ Description Given a ...

  4. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  5. [leetcode] 542. 01 Matrix (Medium)

    给予一个矩阵,矩阵有1有0,计算每一个1到0需要走几步,只能走上下左右. 解法一: 利用dp,从左上角遍历一遍,再从右下角遍历一遍,dp存储当前位置到0的最短距离. 十分粗心的搞错了col和row,改 ...

  6. 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的值. 最 ...

  7. LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

    Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...

  8. 计算机学院大学生程序设计竞赛(2015’12)01 Matrix

    01 Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  9. hdu 01 Matrix

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission ...

随机推荐

  1. intellij idea 2017和Jprofiler 10的集成 报错问题

    本来想用Jprofiler来分析一下自己写的Java项目,以提高代码执行效率和自己的编码能力.结果,官网和网上很多帖子都写了点出session->IDE integrations->选择i ...

  2. ext组件的查询方式

    1.使用id进行查询 (1)Ext.ComponentQuery.query("#mypanel") (2)Ext.getCmp("mypanel") 2.根据 ...

  3. androidkiller连接模拟器并修改源码调试

    首先需要连接模拟器,首先在模拟器的bin目录下运行命令:nox_adb.exe connect 127.0.0.1:62001(可以disconnect关闭): 之后在androidkiller的bi ...

  4. Ecto中的changeset,schema,struct,map

    概要 schema changeset struct map 总结 概要 Ecto 中, 对数据库的操作中经常用到 4 个类型: schema changeset struct map 在 Ecto ...

  5. 【Teradata SQL】日历函数查询

    查询2018年agmt_id=1076226890174464676612的,且金额类型代码为0212,每日协议金额. 1.协议金额历史表取某一日数据(20180101) sel t.start_dt ...

  6. 怪事年年有,今天特别多!org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'empno' not found. Available parameters are [emp, deptno, param1, param

    错误: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.Binding ...

  7. dig请求和回应中的参数解释

    ; <<>> DiG 9.9.5-3ubuntu0.6-Ubuntu <<>> baidu.com dig这个程序的版本号和要查询的域名 ;; glob ...

  8. springmvc上传文件错误The current request is not a multipart request

    <1>在jsp页面的<head></head>标签里面加上<meta http-equiv="Content-Type" content= ...

  9. Neutron:浮动ip

    如果需要从外网直接访问 instance,则可以利用 floating IP.   下面是关于 floating IP 必须知道的事实: 1. floating IP 提供静态 NAT 功能,建立外网 ...

  10. 升级:DNAtools for Excel工具箱,2.x英文版- VBA代码破解工具

    原始出处:www.cnblogs.com/Charltsing/p/DnaTools.html QQ:564955427 DNA工具箱全部功能一览:    单元格焦点指示(支持Excel 2007~2 ...