-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumberofislands.py
33 lines (25 loc) · 921 Bytes
/
numberofislands.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
31
32
33
def numIslands(self, grid: List[List[str]]) -> int:
count = 0
rows = len(grid)
cols = len(grid[0])
''' very similar solution to all these permuation problems
going through each coordinate and changing them if they are
connected
'''
def explore(i, j):
if grid[i][j] == "1":
grid[i][j] = "0"
if i-1 > -1:
explore(i-1, j)
if i+1 < rows:
explore(i+1, j)
if j-1 > -1:
explore(i, j-1)
if j+1 < cols:
explore(i, j+1)
for i in range(rows):
for j in range(cols):
if grid[i][j] == "1":
explore(i, j)
count += 1
return count