Skip to content

Commit

Permalink
Queue改用list,AC
Browse files Browse the repository at this point in the history
  • Loading branch information
rinwf committed Nov 16, 2018
1 parent 91173e5 commit 29fba14
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lc542. 01 Matrix.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down

0 comments on commit 29fba14

Please sign in to comment.