Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit443dfca

Browse files
authored
Update 18-01 Martix
1 parent68ab990 commit443dfca

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

‎18-01 Martix

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,27 @@ class Solution:
1717
q.append((newX, newY))
1818
return matrix
1919

20-
Go through the solution once again
20+
Go through the solution once again ✅
21+
Algo1 repeat
22+
23+
class Solution:
24+
def updateMatrix(self, matrix: List[List[int]]) -> List[List[int]]:
25+
q = deque()
26+
for i in range(len(matrix)):
27+
for j in range(len(matrix[0])):
28+
if matrix[i][j] == 0:
29+
q.append((i, j))
30+
else:
31+
matrix[i][j] = '#' # marked as unvisited
32+
33+
while q:
34+
x, y = q.popleft()
35+
for r, c in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
36+
newX, newY = x+r, y+c
37+
if 0 <= newX < len(matrix) and 0 <= newY < len(matrix[0]) and matrix[newX][newY] == '#':
38+
matrix[newX][newY] = matrix[x][y] + 1
39+
q.append((newX, newY))
40+
return matrix
41+
42+
43+
# if we run bfs from whenever we incounter 1 then the time complexity will be 1 * O(n * m)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp