Number of Islands(union find)
Example 1:
Input:
11110
11010
11000
00000
Output: 1
Example 2:
Input:
11000
11000
00100
00011
Output: 3Last updated
Example 1:
Input:
11110
11010
11000
00000
Output: 1
Example 2:
Input:
11000
11000
00100
00011
Output: 3Last updated
import collections
class Solution:
parent = []
count = 0
def numIslands(self, grid):
"""
:type grid: List[List[str]]
:rtype: int
"""
if not grid or not len(grid[0]):
return 0
n = len(grid)
m = len(grid[0])
self.parent = [-1] * m * n#可以用数组而不是dict,初始化为-1时候,后面find也是-1
# parent = collections.defautdict(int)
for i in range(n):
for j in range(m):
# self.parent[i*m+j] = i*m+j
if grid[i][j] == '1':
self.count += 1
d = [-1, 0, 1, 0, -1]#错半死
for i in range(n):
for j in range(m):
if grid[i][j] == '1':
cur = i * m + j
for di in range(4):
ni = i + d[di]
nj = j + d[di + 1]
if self.isBound(ni,nj,n,m) and grid[ni][nj] == '1':
nn = ni * m + nj
self.union(cur, nn)
return self.count
def find(self, x):
if self.parent[x] == -1:
return x
self.parent[x] = self.find(self.parent[x])
return self.parent[x]
def union(self, x, y):
rx = self.find(x)
ry = self.find(y)
if rx != ry:
self.parent[rx] = ry
self.count -= 1
def isBound(self,x,y,n,m):
if x < 0 or x >= n or y < 0 or y >= m:
return False;
return True;