[LeetCode][Python3] 200. Number of Islands

티스토리 메뉴 펼치기 댓글수0

프로그래밍/LeetCode

[LeetCode][Python3] 200. Number of Islands

snoopybox
댓글수0

Problem :

https://leetcode.com/problems/number-of-islands/


My Solution :

class Solution:
def numIslands(self, grid: 'List[List[str]]') -> 'int':
count = 0

def dfs(row, col):
if grid[row][col] == '0':
return
grid[row][col] = '0'
for y, x in ((row-1, col), (row+1, col),
(row, col-1), (row, col+1)):
if 0 <= y < len(grid) and 0 <= x < len(grid[0]):
dfs(y, x)

for row in range(len(grid)):
for col in range(len(grid[0])):
if grid[row][col] == '1':
count += 1
dfs(row, col)

return count


Comment :

가장 전형적인 DFS 문제 중 하나이다.

맨위로

https://www.snoopybox.co.kr/1960

신고하기