[leetcode] 542. 01 Matrix (Medium)
给予一个矩阵,矩阵有1有0,计算每一个1到0需要走几步,只能走上下左右。
解法一:
利用dp,从左上角遍历一遍,再从右下角遍历一遍,dp存储当前位置到0的最短距离。
十分粗心的搞错了col和row,改了半天…………
Runtime: 132 ms, faster than 98.88% of C++ online submissions for 01 Matrix.
class Solution
{
public:
vector<vector<int>> updateMatrix(vector<vector<int>> &matrix)
{
if (matrix.size() == || matrix[].size() == )
return matrix; int n;
int m;
n = matrix.size();
m = matrix[].size();
int rangeNum = n + m;
vector<vector<int>> dis(n, vector<int>(m, )); for (int i = ; i < n; i++)
for (int j = ; j < m; j++)
{
if (matrix[i][j] == )
dis[i][j] = ;
else
{
int up = (i > ) ? dis[i - ][j] : rangeNum;
int left = (j > ) ? dis[i][j - ] : rangeNum;
dis[i][j] = min(left, up) + ;
}
} for (int i = n - ; i >= ; i--)
for (int j = m - ; j >= ; j--)
{
if (matrix[i][j] == )
dis[i][j] = ;
else
{
int right = (j + ) < m ? dis[i][j + ] : rangeNum;
int down = (i + ) < n ? dis[i + ][j] : rangeNum;
dis[i][j] = min(min(right, down) + , dis[i][j]);
}
}
return dis;
}
};
解法二:
BFS
class Solution
{
private:
bool isValid(int m, int n, int x, int y)
{
return x >= && y >= && x < m && y < n;
} int getShortestDistance(int m, int n, int x, int y, vector<vector<int>> &distance)
{
int result = distance[x][y]; if (isValid(m, n, x, y + ) && distance[x][y + ] != INT_MAX)
{
result = min(result, + distance[x][y + ]);
}
if (isValid(m, n, x, y - ) && distance[x][y - ] != INT_MAX)
{
result = min(result, + distance[x][y - ]);
}
if (isValid(m, n, x + , y) && distance[x + ][y] != INT_MAX)
{
result = min(result, + distance[x + ][y]);
}
if (isValid(m, n, x - , y) && distance[x - ][y] != INT_MAX)
{
result = min(result, + distance[x - ][y]);
}
return result;
} public:
vector<vector<int>> updateMatrix(vector<vector<int>> &matrix)
{
int m = matrix.size();
int n = matrix[].size();
vector<vector<int>> distance(m, vector<int>(n, INT_MAX));
queue<pair<int, int>> visit; for (int i = ; i < m; i++)
{
for (int j = ; j < n; j++)
{
if (matrix[i][j] == )
{
distance[i][j] = ;
visit.push(make_pair(i, j + ));
visit.push(make_pair(i, j - ));
visit.push(make_pair(i + , j));
visit.push(make_pair(i - , j));
}
}
} while (!visit.empty())
{
pair<int, int> cur = visit.front();
visit.pop();
int x = cur.first;
int y = cur.second; if (isValid(m, n, x, y))
{
int shortestD = getShortestDistance(m, n, x, y, distance);
if (shortestD < distance[x][y])
{
distance[x][y] = shortestD;
visit.push(make_pair(x, y + ));
visit.push(make_pair(x, y - ));
visit.push(make_pair(x + , y));
visit.push(make_pair(x - , y));
}
}
}
return distance;
}
};
[leetcode] 542. 01 Matrix (Medium)的更多相关文章
- 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的值. 最 ...
- LeetCode 542. 01 Matrix
输入:只包含0,1的矩阵 输出:元素1到达最近0的距离 算法思想:广度优先搜索. 元素为0为可达区域,元素为1为不可达区域,我们的目标是为了从可达区域不断地扩展至不可达区域,在扩展的过程中,也就计算出 ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- [Leetcode Week10]01 Matrix
01 Matrix 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/01-matrix/description/ Description Given a ...
- Java实现 LeetCode 542 01 矩阵(暴力大法,正反便利)
542. 01 矩阵 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . 示例 1: 输入: 0 0 0 0 1 0 0 0 0 输出: 0 0 0 ...
- [array] leetcode - 54. Spiral Matrix - Medium
leetcode-54. Spiral Matrix - Medium descrition GGiven a matrix of m x n elements (m rows, n columns) ...
- Leetcode 542.01矩阵
01矩阵 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . 示例 1: 输入: 0 0 0 0 1 0 0 0 0 输出: 0 0 0 0 1 0 ...
- 542 01 Matrix 01 矩阵
给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离.两个相邻元素间的距离为 1 .示例 1:输入:0 0 00 1 00 0 0输出:0 0 00 1 00 0 0 示例 2:输入: ...
- 542. 01 Matrix
class Solution { public: vector<vector<int>> res; int m, n; vector<vector<int>& ...
随机推荐
- Windows下libevent C++封装类实现
题记 windows平台下对于服务器高并发的网络模型选型中,使用libevent是个不错的选择. 本文的背景基于:国内博客对于libevent大多介绍linux实现,大多是c语言的实现,Windows ...
- QT使用MySql的配置(使用addLibraryPath载入插件),编译QT的MySql驱动问题及解决方案(自己使用libmysql.lib进行编译mysql.pro,万不得已可以查看Makefile.Debug以解决问题)
2010/04/23:Fixes : 更新批处理,以兼容WIN7. 第一次系统地玩QT,于是诞生了此预备式: [QT版本4.6.0(VS2008编译版),开发平台推荐使用Qt Creator(最新1. ...
- 取得文件夹内容信息(使用IShellFolder接口)
翻译自MSDN 2005 -> Win32 和 COM 开发 -> User Interface -> Windows User Experience -> Windows S ...
- ngnix 安装
1安装PCRE库 ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 下载最新的 PCRE 源码包,使用下面命令下载编译和安装 PCRE 包: ...
- python算法与数据结构-单链表(38)
一.链表 链表是一种物理存储单元上非连续.非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的.链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成.每个结点包括 ...
- Free MP3 CD Ripper_缓冲区溢出远程代码执行_CVE-2019-9766漏洞复现
Free MP3 CD Ripper_缓冲区溢出远程代码执行_CVE-2019-9766漏洞复现 一.漏洞描述 Free MP3 CD Ripper是一款音频格式转换器.Free MP3 CD Rip ...
- spring boot 2.x 系列 —— spring boot 整合 redis
文章目录 一.说明 1.1 项目结构 1.2 项目主要依赖 二.整合 Redis 2.1 在application.yml 中配置redis数据源 2.2 封装redis基本操作 2.3 redisT ...
- spring cloud 系列第6篇 —— zuul 服务网关 (F版本)
源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.zuul简介 1.1 API 网关 api 网关是整个微服务系统的门面 ...
- js中新增动态属性
var cc = 'hell' var mm = { [cc](){ alert(33) } } mm.hell() 使用的就是数组形式
- Node.js Windows Example
Firstly, download the msi file from https://nodejs.org/en/ Second, click the msi file to install nod ...