zoukankan      html  css  js  c++  java
  • [LeetCode] 542. 01 矩阵

    有大致的思路,想到的方法类似于动态规划,但是写不出代码来

    也没想到用队列来做。

    看了高分评论:

     方法一:

    class Solution {
        public int[][] updateMatrix(int[][] matrix) {
            // 首先将所有的 0 都入队,并且将 1 的位置设置成 -1,表示该位置是 未被访问过的 1
            Queue<int[]> queue = new LinkedList<>();
            int m = matrix.length, n = matrix[0].length;
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    if (matrix[i][j] == 0) {
                        queue.offer(new int[] {i, j});
                    } else {
                        matrix[i][j] = -1;
                    } 
                }
            }
            
            int[] dx = new int[] {-1, 1, 0, 0};
            int[] dy = new int[] {0, 0, -1, 1};
            while (!queue.isEmpty()) {
                int[] point = queue.poll();
                int x = point[0], y = point[1];
                for (int i = 0; i < 4; i++) {
                    int newX = x + dx[i];
                    int newY = y + dy[i];
                    // 如果四邻域的点是 -1,表示这个点是未被访问过的 1
                    // 所以这个点到 0 的距离就可以更新成 matrix[x][y] + 1。
                    if (newX >= 0 && newX < m && newY >= 0 && newY < n 
                            && matrix[newX][newY] == -1) {
                        matrix[newX][newY] = matrix[x][y] + 1;
                        queue.offer(new int[] {newX, newY});
                    }
                }
            }
    
            return matrix;
        }
    }

    方法二:

    class Solution {
        public int[][] updateMatrix(int[][] matrix) {
            // 首先将 0 边上的 1 入队
            int[] dx = new int[] {-1, 1, 0, 0};
            int[] dy = new int[] {0, 0, -1, 1};
            Queue<int[]> queue = new LinkedList<>();
            int m = matrix.length, n = matrix[0].length;
            int[][] res = new int[m][n];
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    if (matrix[i][j] == 0) {
                        for (int k = 0; k < 4; k++) {
                            int x = i + dx[k];
                            int y = j + dy[k];
                            if (x >= 0 && x < m && y >= 0 && y < n 
                                    && matrix[x][y] == 1 && res[x][y] == 0) {
                                // 这是在 0 边上的1。需要加上 res[x][y] == 0 的判断防止重复入队
                                res[x][y] = 1;
                                queue.offer(new int[] {x, y});
                            }
                        }
                    }
                }
            }
    
            while (!queue.isEmpty()) {
                int[] point = queue.poll();
                int x = point[0], y = point[1];
                for (int i = 0; i < 4; i++) {
                    int newX = x + dx[i];
                    int newY = y + dy[i];
                    if (newX >= 0 && newX < m && newY >= 0 && newY < n 
                            && matrix[newX][newY] == 1 && res[newX][newY] == 0) {
                        res[newX][newY] = res[x][y] + 1;
                        queue.offer(new int[] {newX, newY});
                    }
                }
            }
    
            return res;
        }
    }

    动态规划方法需要再看看!!!

  • 相关阅读:
    基于Swoole的HTTP/HTTPS代理
    Java9新特性系列(module&maven&starter)
    Java9新特性系列(module&maven&starter)
    Java9新特性系列(module&maven&starter)
    Java9新特性系列(module&maven&starter)
    RxJava2源码解析(二)
    C#中的委托和事件(续)
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
  • 原文地址:https://www.cnblogs.com/doyi111/p/12709701.html
Copyright © 2011-2022 走看看