Skip to content

Commit 4835515

Browse files
committed
leetcode.com 200. Number of Islands
문제 링크: https://leetcode.com/problems/number-of-islands
1 parent 414fae7 commit 4835515

File tree

1 file changed

+26
-0
lines changed
  • leetcode.com 200. Number of Islands v2

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def numIslands(self, grid: List[List[str]]) -> int:
6+
MAX_ROW = len(grid)
7+
MAX_COL = len(grid[0])
8+
answer = 0
9+
drc = [[-1, 0], [0, 1], [1, 0], [0, -1]]
10+
11+
for row in range(MAX_ROW):
12+
for col in range(MAX_COL):
13+
if grid[row][col] == '1':
14+
answer += 1
15+
queue = [[row, col]]
16+
grid[row][col] = '0'
17+
18+
while queue:
19+
sr, sc = queue.pop(0)
20+
21+
for dr, dc in drc:
22+
r, c = sr + dr, sc + dc
23+
if 0 <= r < MAX_ROW and 0 <= c < MAX_COL and grid[r][c] == '1':
24+
queue.append([r, c])
25+
grid[r][c] = '0'
26+
return answer

0 commit comments

Comments
 (0)