[LeetCode][Python3] 200. Number of Islands
2019. 2. 11. 00:39 |
프로그래밍/LeetCode
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 문제 중 하나이다.
'프로그래밍 > LeetCode' 카테고리의 다른 글
[LeetCode][Python3] 131. Palindrome Partitioning (0) | 2019.03.23 |
---|---|
[LeetCode][Python3] 128. Longest Consecutive Sequence (0) | 2019.03.23 |
[LeetCode][Python3] 42. Trapping Rain Water (0) | 2019.03.23 |
[LeetCode][Python] 341. Flatten Nested List Iterator (0) | 2019.03.22 |
[LeetCode][Python3] 300. Longest Increasing Subsequence (0) | 2019.02.10 |
[LeetCode][Python3] 240. Search a 2D Matrix II (0) | 2019.02.09 |
[LeetCode][Python3] 103. Binary Tree Zigzag Level Order Traversal (0) | 2019.02.07 |
[LeetCode][Python3] 279. Perfect Squares (0) | 2019.01.31 |
최근에 달린 댓글 최근에 달린 댓글