[LeetCode] 200. Number of Islands

김민우·2022년 9월 22일
0

알고리즘

목록 보기
15/189

- Problem

200. Number of Islands

- 내 풀이

class Solution:
    def numIslands(self, grid: List[List[str]]) -> int:
        m, n = len(grid), len(grid[0])
        answer = 0
        
        def bfs(x, y):
            q = collections.deque([[x, y]])
            
            while q:
                x, y = q.popleft()
                
                for dx, dy in ((0, 1), (0, -1), (1, 0), (-1, 0)):
                    nx = x + dx
                    ny = y + dy
                    
                    if 0 <= nx < m and 0 <= ny < n and grid[nx][ny] == "1":
                        grid[nx][ny] = "0"
                        q.append([nx, ny])
            
            return 1
        
        for i in range(m):
            for j in range(n):
                if grid[i][j] == "1":
                    answer += bfs(i, j)
                    
        return answer

- 결과

아 시간 뭔데

profile
Pay it forward.

0개의 댓글