【LeetCode】01 Matrix 解题报告

标签(空格分隔): LeetCode


题目地址: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:

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.

Ways

这个题的思想是BFS,广度优先遍历。也是让我学习了一下到底怎么写BFS。

首先,BFS要用到了队列,让值为0的节点全部进入队列,代表要进行遍历的点;把值为1的点设为最大值,表示距离很远,初始状态下不能到。然后对于队列中的每个点都有四个方向,要考虑这个点临近的四个方向的点距都离为当前点到0点的距离加1.有点类似DP,把某个点到0的距离设为周围点到0的最短值+1即可。注意,遍历时修改完另外点的值的时候,一定要把这个节点也加入到队列中。这个点到0的距离是最近距离+1,而不是单纯的1.

public class Solution {
public List<List<Integer>> updateMatrix(List<List<Integer>> matrix) {
if(matrix == null || matrix.size() == 0){
return matrix;
}
int m = matrix.size();
int n = matrix.get(0).size();
Queue<int[]> queue = new LinkedList<>();
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(matrix.get(i).get(j)==0){
queue.offer(new int[]{i, j});
}else{
matrix.get(i).set(j, Integer.MAX_VALUE);
}
}
}
int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
while(!queue.isEmpty()){
int cell[]= queue.poll();
for(int[] d: dirs){
int r = cell[0] + d[0];
int c = cell[1] + d[1];
if (r < 0 || r >= m || c < 0 || c >= n ||
matrix.get(r).get(c) <= matrix.get(cell[0]).get(cell[1]) + 1)
continue;
queue.offer(new int[]{r, c});
matrix.get(r).set(c, matrix.get(cell[0]).get(cell[1]) + 1);
}
}
return matrix;
}
}

Date

2017 年 4 月 11 日

【LeetCode】01 Matrix 解题报告的更多相关文章

  1. LeetCode: Spiral Matrix 解题报告

    Spiral MatrixGiven a matrix of m x n elements (m rows, n columns), return all elements of the matrix ...

  2. 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)

    [LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

  3. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  4. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  5. LeetCode - Course Schedule 解题报告

    以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...

  6. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  7. 【LeetCode】566. Reshape the Matrix 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 变长数组 求余法 维护行列 相似题目 参考资料 日期 ...

  8. 【LeetCode】519. Random Flip Matrix 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/random-fl ...

  9. 【LeetCode】766. Toeplitz Matrix 解题报告

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:两两比较 方法二:切片相等 方法三:判断每条 ...

随机推荐

  1. PHP识别二维码(php-zbarcode)

    PHP识别二维码(php-zbarcode) 标签: php二维码扩展 2015-11-06 17:12 609人阅读 评论(0) 收藏 举报  分类: PHP(1)  Linux 版权声明:本文为博 ...

  2. java四则运算规则

    java四则运算规则 1.基本规则 运算符:进行特定操作的符号.例如:+ 表达式:用运算符连起来的式子叫做表达式.例如:20 + 5.又例如:a + b 四则运算: 加:+ 减:- 乘:* 除:/ 取 ...

  3. Java8 Lambda表达式、函数式接口和方法引用

    目录 Java8 Lambda表达式和函数式接口 Lambda表达式 Lambda的使用 函数式接口FunctionalInterface Java内置四大核心函数式接口 方法引用 构造器引用 Jav ...

  4. A Child's History of England.11

    CHAPTER 4 ENGLAND UNDER ATHELSTAN AND THE SIX BOY-KINGS Athelstan, the son of Edward the Elder, succ ...

  5. day 03Linux修改命令提示符

    day 03Linux修改命令提示符 昨日回顾 1.选择客户机操作系统: Microsoft Windows # 一次只能安装一台电脑 Linux(推荐) VMware ESX # 服务器版本VNwa ...

  6. Scala(三)【函数式编程】

    目录 一.方法和函数 1.方法 1)基本语法 2)简化原则 3)方法参数 2.函数 3.方法和函数的区别 二.高阶函数 三.匿名函数 四.柯里化 五.闭包 一.方法和函数 1.方法 1)基本语法 de ...

  7. 【Git项目管理】分布式 Git - 分布式工作流程

    分布式 Git - 分布式工作流程 你现在拥有了一个远程 Git 版本库,能为所有开发者共享代码提供服务,在一个本地工作流程下,你也已经熟悉了基本 Git 命令.你现在可以学习如何利用 Git 提供的 ...

  8. swift设置导航栏item颜色和状态栏颜色

    //swift设置导航栏item颜色和状态栏颜色 let dict:Dictionary =[NSForegroundColorAttributeName:UIColor.hrgb("333 ...

  9. Simulating final class in C++

    Ever wondered how can you design a class in C++ which can't be inherited. Java and C# programming la ...

  10. 【Linux】【Services】【KVM】安装与简单配置

    1. 环境: 1.1. OS: Red Hat Enterprise Linux Server release 7.4 (Maipo) 1.2. Kernel: 3.10.0-693.el7.x86_ ...