LeetCode – 01 Matrix

题目:

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.

解题:

1.  BFS:

  •   遍历矩阵将所有值为0的位置放入队列, 对于值1 的点设置为INT_MAX(未遍历标识)
  •    从queue中取出数字, 遍历周围四个点,越界或者小于当前值+1不更新,由于当前值不会大于INT_MAX(10000个),所有已遍历标识(INT_MAX)都大于当前值,需要设置新值。
  • 设置完毕新值后加入队列,继续向周围扩散判断。

实现:

class Solution {
public:
    vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
        int m = matrix.size();
        int n = matrix[0].size();
        vector<vector<int>> dirs{{0,-1},{-1,0},{0,1},{1,0}};
        queue<pair<int, int>> q;
        
        for (int i=0; i<m; i++){
            for (int j=0; j<n; j++){
                if (matrix[i][j] == 0){
                    q.push({i, j});
                }
                else{
                    matrix[i][j] = INT_MAX;
                }
            }
        }
        
        while(!q.empty()) {
            pair<int, int> t = q.front();
            q.pop();
            for (auto dir: dirs){
                int x = t.first + dir[0];
                int y = t.second + dir[1];
                if (x<0 || x>=m || y<0 || y>=n ||\
                    matrix[x][y] <= matrix[t.first][t.second] +1){
                    
                    continue;
                }
                else {
                    matrix[x][y] = matrix[t.first][t.second] + 1;
                    q.push({x,y});
                }
            }
        }
        return matrix;
    }
};

2. 动态规划

  • 初始化非0值未INT_MAX -1标识未达到
  • 两次遍历左,上方向:取其中较小值+1,  第二次遍历右,下方向:排除0,1值后,取其中较小值+1

实现:

class Solution {
public:
    vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
        int m = matrix.size();
        int n = matrix[0].size();
        vector<vector<int>> res(m, vector<int>(n, INT_MAX-1));
      
        for (int i=0; i<m; i++){
            for (int j=0; j<n; j++){
                if (matrix[i][j] == 0){
                      res[i][j] = 0;
                }
                else{
                    if (i > 0){
                        res[i][j] = min(res[i][j], res[i-1][j]+1);
                    }
                    if (j > 0){
                        res[i][j] = min(res[i][j], res[i][j-1]+1);
                    }
                }
            }
        }
             
        for (int i=m-1 ;i >=0 ; i--){
            for (int j= n-1; j>=0; j--){
                if (res[i][j]!=0 && res[i][j]!=1){
                    if (i < m-1) {
                        res[i][j] =  min(res[i][j], res[i+1][j]+1);
                    }
                    if (j < n-1) {
                        res[i][j] =  min(res[i][j], res[i][j+1]+1);
                    }
                }
            }
        }
        
        return res;
    }
};

参考引用:

[LeetCode] 01 Matrix 零一矩阵

花花酱 LeetCode 542. 01 Matrix

 

Be First to Comment

发表评论

电子邮件地址不会被公开。 必填项已用*标注