原题链接在这里:https://leetcode.com/problems/01-matrix/description/

题目:

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

The distance between two adjacent cells is 1.

Example 1: 
Input:

0 0 0
0 1 0
0 0 0

Output:

0 0 0
0 1 0
0 0 0

Example 2: 
Input:

0 0 0
0 1 0
1 1 1

Output:

0 0 0
0 1 0
1 2 1

Note:

  1. The number of elements of the given matrix will not exceed 10,000.
  2. There are at least one 0 in the given matrix.
  3. The cells are adjacent in only four directions: up, down, left and right.

题解:

先把非0点标记成Integer.MAX_VALUE. 然后从每个0点开始做BFS. 若是对周围的点更新出更小的值就把更小值放入queue中用作将来的BFS.

Time Complexity: O(m*n). m = matrix.length, n = matrix[0].length. 每个点最多进入queue 4 次.

Space: O(m*n). queue size.

AC Java:

 1 class Solution {
 2     public int[][] updateMatrix(int[][] matrix) {
 3         if(matrix == null || matrix.length == 0 || matrix[0].length == 0){
 4             return matrix;
 5         }
 6
 7         int m = matrix.length;
 8         int n = matrix[0].length;
 9
10         LinkedList<int []> que = new LinkedList<int []>();
11         for(int i = 0; i<m; i++){
12             for(int j = 0; j<n; j++){
13                 if(matrix[i][j] == 0){
14                     que.add(new int[]{i, j});
15                 }else{
16                     matrix[i][j] = Integer.MAX_VALUE;
17                 }
18             }
19         }
20
21         int [][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
22
23         while(!que.isEmpty()){
24             int [] cur = que.poll();
25             for(int [] dir : dirs){
26                 int dx = cur[0] + dir[0];
27                 int dy = cur[1] + dir[1];
28                 if(dx<0 || dx>=m || dy<0 || dy>=n || matrix[cur[0]][cur[1]]+1>=matrix[dx][dy]){
29                     continue;
30                 }
31
32                 matrix[dx][dy] = matrix[cur[0]][cur[1]]+1;
33                 que.add(new int[]{dx, dy});
34             }
35         }
36
37         return matrix;
38     }
39 }

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/9602413.html

LeetCode 542. 01 Matrix相关推荐

  1. leetcode 542. 01 Matrix | 542. 01 矩阵(图解,广度优先搜索)

    题目 https://leetcode.com/problems/01-matrix/ 题解 这题很有趣,图解一下思路吧~ 可以想象成"感染"的过程. 从 1 开始逐层向外扩散,感 ...

  2. LeetCode 542. 01 Matrix--C++解法--动态规划

    LeetCode 542. 01 Matrix–C++解法–动态规划 LeetCode题解专栏:LeetCode题解 LeetCode 所有题目总结:LeetCode 所有题目总结 大部分题目C++, ...

  3. LeetCode 542. 01 矩阵

    542. 01 矩阵 笔记 x*col+y这里卡了很久 class Solution { public:vector<vector<int>> updateMatrix(vec ...

  4. LeetCode 542. 01 矩阵(BFS DP)

    文章目录 1. 题目 2. 解题 2.1 BFS 2.2 DP动态规划 1. 题目 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . 示例 1: ...

  5. Leetcode - 542. 01 矩阵

    这道题目最重要的是多源BFS,然后今天被一个大佬的文章点醒,很多代码是需要背的!多源BFS也不例外.一定要背!!!! 大佬的链接(代码模板):https://blog.csdn.net/fuxuemi ...

  6. 542. 01 Matrix

    输入:元素值为0或者1的矩阵. 输出: 每个元素距离0的最近距离是多少. 规则:相邻单元格的距离是1,相邻是指上下左右4个方向. 分析:这类似于学习课程安排,可以从元素值为0的单元开始沿4个方向遍历. ...

  7. LeetCode 542 01 矩阵

    题目描述 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . 题解 广度优先搜索 代码 class Solution {public:vector& ...

  8. LeetCode 59 Spiral Matrix II(螺旋矩阵II)(Array)

    版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5214 ...

  9. LeetCode 73. Set Matrix Zeroes

    LeetCode 73. Set Matrix Zeroes Solution1:我的答案 比较笨,算法时间复杂度是O(mn)O(mn)O(mn),占用额外空间是O(m+n)O(m+n)O(m+n) ...

最新文章

  1. ISME:宿主性别可以决定肠道微生物对寄生虫感染的响应
  2. 一个直接运行Windows命令行的软件
  3. Java SE 6 新特性: Java DB 和 JDBC 4.0
  4. 关于 SAP 电商云 Spartacus UI 路由 routes 配置的数据源问题
  5. U66785 行列式求值
  6. 黑匣子解密要多久_“黑匣子”揭秘
  7. 特斯拉得克萨斯超级工厂正式开业 计划明年开始生产电动皮卡
  8. HDU4686——Arc of Dream矩阵快速幂
  9. 不用空格怎么打两个空格_为什么在寸土寸金的键盘上,空格键却要做这么长,究竟怎么回事?...
  10. protel99se简明实用手册
  11. Tableau Desktop 10.4.2 的安装和激活
  12. 程序员如何删库?(一看就会)Linux
  13. 饭店点餐系统的设计与实现
  14. Qt2D游戏开发引擎QtGameEngine使用入门5——创建一个敌对的怪物角色并自动攻击
  15. [置顶]使用scrapy_redis,自动实时增量更新东方头条网全站新闻
  16. l10n i18n vue_带有Vue的更多i18n:格式和后备
  17. 对鸡你太美的字符画绘制
  18. 域策略(2)——设置统一桌面背景
  19. 二叉树(Binary Trees)
  20. 手脫 -- PECompact 2.x -gt; Jeremy Collake

热门文章

  1. 董监高股票减持25%规定?
  2. 为什么新闻联播的主持人不低头念稿,难道都背下来了?
  3. 是不是每个人的成长路上都会有迷茫的阶段?
  4. 商业认知,近期与部分中小创业者一起吃饭,忽然有人谈到现在创业越来越难
  5. 为什么创业你只为少数人服务就够了?
  6. 内核如何为系统调用的参数传递参数
  7. 如何看待小米10的葫芦屏?
  8. The name Foxit Software sounds familiar
  9. php取mysql某列的值,php – 获取MYSQL中某些列为null的表中的值
  10. java excel 电话号码_数值或者电话号码被EXCEL转成了科学计数法,用XSSFCell 如何读取...