【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. nginx 文件目录页面

    开启文件目录 server { listen 80; #设置自己静态目录的访问域名 server_name xxx.xxxx.com; #防止页面中文乱码一定要在utf-8前面加上gbk,顺序很重要 ...

  2. 40-3Sum Closest

    3Sum Closest My Submissions QuestionEditorial Solution Total Accepted: 76185 Total Submissions: 2621 ...

  3. Python压缩&解压缩

    Python中常用的压缩模块有zipfile.tarfile.gzip 1.zipfile模块的简单使用 import zipfile # 压缩 z1 = zipfile.ZipFile('zip_t ...

  4. 使用Openmp并行化

    运行命令:g++ -fopenmp xx.cpp -lgomp -lpthread -o xx.out 用例一: #include <omp.h> #include <stdio.h ...

  5. “equals”有值 与 “==”存在 “equals”只是比较值是否相同,值传递,==地址传递,null==a,避免引发空指针异常,STRING是一个对象==null,对象不存在,str.equals("")对象存在但是包含字符‘''

    原文链接:http://www.cnblogs.com/lezhou2014/p/3955536.html "equals" 与 "==" "equa ...

  6. vivo 敏感词匹配系统的设计与实践

    一.前言 谛听系统是vivo的内容审核平台,保障了vivo各互联网产品持续健康的发展.谛听支持审核多种内容类型,但日常主要审核的内容是文本,下图是一个完整的文本审核流程,包括名单匹配.敏感词匹配.AI ...

  7. C语言中的指针与整数相加的值计算

    以下分三种情况: 1. 指针 + 整数值 2. 整数 + 整数  3. 指针强制转换为另一个类型后(指针或者是整数)  +  整数 测试例子: 1 struct AAA{ int a; char b[ ...

  8. HDFS初探之旅(一)

    1.HDFS简介                                                                                            ...

  9. 视频框架 Vitamio使用

    转自http://blog.csdn.net/u010181592/article/category/5893483 1.在https://github.com/yixia/VitamioBundle ...

  10. clickhouse 输入输出格式

    TabSeparated.TabSeparatedRaw.TabSeparatedWithNames和TabSeparatedWithNamesAndTypes TabSeparated 默认格式,缩 ...