forked from Garvit244/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
200.py
30 lines (25 loc) · 720 Bytes
/
200.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution(object):
def numIslands(self, grid):
"""
:type grid: List[List[str]]
:rtype: int
"""
if not grid:
return 0
count = 0
for row in range(len(grid)):
for col in range(len(grid[0])):
if grid[row][col] == '1':
count +=1
self.merge(grid, row, col)
return count
def merge(self, grid, row, col):
if 0 > row or row >= len(grid) or col < 0 or col >= len(grid[0]):
return
if grid[row][col] != '1':
return
grid[row][col] = '#'
self.merge(grid, row+1, col)
self.merge(grid, row-1, col)
self.merge(grid, row, col+1)
self.merge(grid, row, col-1)