-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path200. Number of Islands.py
41 lines (31 loc) · 1.32 KB
/
200. Number of Islands.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
34
35
36
37
38
39
40
41
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
m,n = len(grid), len(grid[0])
setMap = [0] * (n*m)
groupCount = 0
for row in range(m):
for col in range(n):
if grid[row][col] == "1":
ind = self.getIndexFromRC(n,row,col)
setMap[ind] = ind
groupCount += 1
if row > 0 and grid[row-1][col] == "1":
groupCount -= self.union(setMap,ind, self.getIndexFromRC(n,row-1,col))
if col > 0 and grid[row][col-1] == "1":
groupCount -= self.union(setMap,ind,self.getIndexFromRC(n,row,col-1))
return groupCount
def find(self, setMap: List[int], ind: int) -> int:
if setMap[ind] == ind:
return ind
group = self.find(setMap, setMap[ind])
setMap[ind] = group
return group
def union(self, setMap: List[int], u: int, v: int) -> int:
groupU = self.find(setMap, u)
groupV = self.find(setMap, v)
if groupU == groupV:
return 0
setMap[groupU] = groupV
return 1
def getIndexFromRC(self,n: int, row: int, col: int) -> int:
return n*row + col