From 29fba148734e3c05c680ea4378e05365d53e8ea5 Mon Sep 17 00:00:00 2001 From: Azureki Date: Fri, 16 Nov 2018 23:23:33 +0800 Subject: [PATCH] =?UTF-8?q?Queue=E6=94=B9=E7=94=A8list=EF=BC=8CAC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lc542. 01 Matrix.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lc542. 01 Matrix.py b/lc542. 01 Matrix.py index 11abcff..4e1b615 100644 --- a/lc542. 01 Matrix.py +++ b/lc542. 01 Matrix.py @@ -1,28 +1,29 @@ -from queue import Queue +# from queue import Queue class Solution: def updateMatrix(self, matrix): width = len(matrix[0]) height = len(matrix) - q = Queue() + q=[] dirs = [(-1, 0), (1, 0), (0, 1), (0, -1)] for i in range(height): for j in range(width): if matrix[i][j] == 0: - q.put((i, j)) + q.append((i, j)) else: matrix[i][j] = float('inf') - while not q.empty(): - tem = q.get() + # while not q.empty(): + # tem = q.get() + for tem in q: for d in dirs: x, y = tem[0] + d[0], tem[1] + d[1] if x < 0 or x >= height or y < 0 or y >= width or matrix[x][y] <= matrix[tem[0]][tem[1]]: continue else: matrix[x][y] = matrix[tem[0]][tem[1]] + 1 - q.put((x, y)) + q.append((x, y)) return matrix