Leetcode--542. 01 矩阵(java)
给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离。
两个相邻元素间的距离为 1 。
示例 1:
输入:
0 0 0
0 1 0
0 0 0
输出:
0 0 0
0 1 0
0 0 0
示例 2:
输入:
0 0 0
0 1 0
1 1 1
输出:
0 0 0
0 1 0
1 2 1
注意:
给定矩阵的元素个数不超过 10000。
给定矩阵中至少有一个元素是 0。
矩阵中的元素只在四个方向上相邻: 上、下、左、右。
代码:
方法一:动态规划
class Solution {
public int[][] updateMatrix(int[][] matrix) {
int dp[][] = new int[matrix.length][matrix[0].length];
if(matrix.length==0){
return dp;
}
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[0].length;j++){
if(matrix[i][j]==1){
dp[i][j]=matrix.length+matrix[0].length;
}
}
}
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[0].length;j++){
if(i>0){
dp[i][j] = Math.min(dp[i][j],dp[i-1][j]+1);
}
if(j>0){
dp[i][j] = Math.min(dp[i][j],dp[i][j-1]+1);
}
}
}
for(int i=matrix.length-1;i>=0;i--){
for(int j=0;j<matrix[0].length;j++){
if(i<matrix.length-1){
dp[i][j] = Math.min(dp[i][j],dp[i+1][j]+1);
}
if(j>0){
dp[i][j] = Math.min(dp[i][j],dp[i][j-1]+1);
}
}
}
for(int i=matrix.length-1;i>=0;i--){
for(int j=matrix[0].length-1;j>=0;j--){
if(i<matrix.length-1){
dp[i][j] = Math.min(dp[i][j],dp[i+1][j]+1);
}
if(j<matrix[0].length-1){
dp[i][j] = Math.min(dp[i][j],dp[i][j+1]+1);
}
}
}
for(int i=0;i<matrix.length;i++){
for(int j=matrix[0].length-1;j>=0;j--){
if(i>0){
dp[i][j] = Math.min(dp[i][j],dp[i-1][j]+1);
}
if(j<matrix[0].length-1){
dp[i][j] = Math.min(dp[i][j],dp[i][j+1]+1);
}
}
}
return dp;
}
}
方法二:BFS
class Solution {
int[][] vector = new int[][]{{0,1},{0,-1},{1,0},{-1,0}};
public int[][] updateMatrix(int[][] matrix) {
if(matrix.length==0){
return 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]==1){
matrix[i][j]=matrix.length+matrix[0].length;
}else{
queue.add(new int[]{i,j});
}
}
}
while(!queue.isEmpty()){
int []s = queue.poll();
for(int[] v:vector){
int r = s[0]+v[0],c=s[1]+v[1];
if(r>=0&&r<matrix.length&&c>=0&&c<matrix[0].length&&matrix[s[0]][s[1]]+1<matrix[r][c]){
matrix[r][c] = matrix[s[0]][s[1]]+1;{
queue.add(new int[]{r,c});
}
}
}
}
return matrix;
}
}
Leetcode--542. 01 矩阵(java)相关推荐
- LeetCode 542. 01 矩阵
542. 01 矩阵 笔记 x*col+y这里卡了很久 class Solution { public:vector<vector<int>> updateMatrix(vec ...
- LeetCode 542. 01 矩阵(BFS DP)
文章目录 1. 题目 2. 解题 2.1 BFS 2.2 DP动态规划 1. 题目 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . 示例 1: ...
- Leetcode - 542. 01 矩阵
这道题目最重要的是多源BFS,然后今天被一个大佬的文章点醒,很多代码是需要背的!多源BFS也不例外.一定要背!!!! 大佬的链接(代码模板):https://blog.csdn.net/fuxuemi ...
- LeetCode 542 01 矩阵
题目描述 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . 题解 广度优先搜索 代码 class Solution {public:vector& ...
- LeetCode 542. 01 Matrix--C++解法--动态规划
LeetCode 542. 01 Matrix–C++解法–动态规划 LeetCode题解专栏:LeetCode题解 LeetCode 所有题目总结:LeetCode 所有题目总结 大部分题目C++, ...
- leetcode 542. 01 Matrix | 542. 01 矩阵(图解,广度优先搜索)
题目 https://leetcode.com/problems/01-matrix/ 题解 这题很有趣,图解一下思路吧~ 可以想象成"感染"的过程. 从 1 开始逐层向外扩散,感 ...
- LeetCode 542. 01 Matrix
原题链接在这里:https://leetcode.com/problems/01-matrix/description/ 题目: Given a matrix consists of 0 and 1, ...
- [Leedcode][JAVA][第542题][01矩阵][BFS]
[问题描述]542. 01 矩阵 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . 示例 : 输入: 0 0 0 0 1 0 1 1 1 输出: ...
- 15.使用一样的BFS方法(代码几乎一模一样)解决多道LeetCode题目--542题(01矩阵)1765题(地图中的最高点)994腐烂的橘子
一.综述 本文总结自己刷LeetCode中遇到的使用BFS方法解决相同类型的题目.(代码几乎是一模一样) 二.01矩阵(LeetCode第542题) class Solution {int[][] d ...
最新文章
- 波士顿动力发布新版人形机器人:能跳跃旋转、后空翻
- 【Ubuntu入门到精通系列讲解】Ubuntu 图形界面入门
- python游戏脚本实例-基于Python实现的扫雷游戏实例代码
- Python行业晋升路线如何规划
- JavaScript 技术篇-textContent获取dom节点text文本内容带空格,js获取带空格的dom节点内容
- 数据中心母线布置方式
- 数据中心进水了怎么办?数据中心如何防洪?
- 注册中心ZooKeeper、Eureka、Consul 对比
- OpenGL中的几何体实例化(OpenGL Geometry Instancing)
- Kubernetes事件离线工具kube-eventer正式开源
- 如何把极坐标化为直角坐标_2019高考100题之063(极坐标)
- C语言 const、volatile、const volatile限定符理解
- 为什么不能根据返回类型来区分重载_Java重载和重写的区别
- NLP学习—19.检索式对话系统中的rank评估指标、rank技术(Point-wise/Pair-wise/List-wise)以及RankNet、LambdaRank、LambdaMART
- ERP是属于系统还是软件?
- 一位百度AI工程师的求职经历(offer/面经/干货/感悟)
- linux怎么修改ftp虚拟用户账号密码,Linux下FTP虚拟账户配置
- The Fewest Coins(多重背包+完全背包)
- MongoDB+Node.js+express简单实现数据的提交与回显
- 使用stream流进行集合排序取最大值,根据集合中的bigdemal属性排序(正序反序)并取最大值
热门文章
- LeetCode 1480. 一维数组的动态和(前缀和)
- LeetCode 1304. 和为零的N个唯一整数
- LeetCode 31. 下一个排列(线性扫描)
- 程序员面试金典 - 面试题 16.15. 珠玑妙算(map计数)
- 网站搜索功能怎么实现_电商网站上的搜索功能是如何实现的?
- Spring Boot中使用Spring-data-jpa让数据访问更简单、更优雅
- Spring Cloud Stream如何处理消息重复消费
- Java多线程系列(六):深入详解Synchronized同步锁的底层实现
- 论文浅尝 - ACL2020 | IntKB: 一种交互式知识图谱补全框架
- 论文浅尝 | 基于知识库的自然语言理解 03#