Leetcode 542 01矩阵 图的按距离遍历
按距离进行 BFS 即可,JAVA:
int[][] dir = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; public final int[][] updateMatrix(int[][] matrix) { if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return new int[0][0]; int xLen = matrix.length, yLen = matrix[0].length; Set<Long> set = new HashSet<Long>(); Queue<int[]> queue = new LinkedList<int[]>(); for (int i = 0; i < xLen; i++) { for (int j = 0; j < yLen; j++) { if (matrix[i][j] == 0) { queue.add(new int[]{i, j}); set.add(((long) i) << 32 | (long) j); } ; } } int currentNum = 1; while (queue.size() > 0) { int size = queue.size(); for (int i = 0; i < size; i++) { int[] current = queue.poll(); int x = current[0], y = current[1]; for (int j = 0; j < 4; j++) { int dX = x + dir[j][0], dY = y + dir[j][1]; long key = ((long) dX) << 32 | (long) dY; if (dX >= xLen || dX < 0 || dY >= yLen || dY < 0 || set.contains(key)) continue; matrix[dX][dY] = currentNum; queue.add(new int[]{dX, dY}); set.add(key); } } currentNum++; } return matrix; }
JS,在构造二维矩阵时要慎用 fill 方法,fill 方法中如果填充内容是对象,所有元素将指向同一对象:
/** * @param {number[][]} matrix * @return {number[][]} */ var updateMatrix = function (matrix) { if (!matrix || matrix.length == 0 || matrix[0].length == 0) return matrix; let dir = [[1, 0], [-1, 0], [0, 1], [0, -1]]; let queue = []; let xLen = matrix.length, yLen = matrix[0].length; let his = new Array(xLen); for (let i = 0; i < xLen; i++) his[i] = new Array(yLen).fill(0); for (let i = 0; i < xLen; i++) { for (let j = 0; j < yLen; j++) { if (matrix[i][j] == 0) { queue.push([i, j]); his[i][j] = 1; } } } let currentNum = 1; while (queue.length > 0) { let currentLen = queue.length; for (let i = 0; i < currentLen; i++) { let currentNode = queue.shift(); let x = currentNode[0], y = currentNode[1]; for (let j = 0; j < 4; j++) { let dX = x + dir[j][0], dY = y + dir[j][1]; if (dX < 0 || dX >= xLen || dY < 0 || dY >= yLen || his[dX][dY] == 1) continue; matrix[dX][dY] = currentNum; his[dX][dY] = 1; queue.push([dX, dY]); } } currentNum++; } return matrix; }
当你看清人们的真相,于是你知道了,你可以忍受孤独