Number of Islands
200. Number of Islands
def numIslands(self, grid: List[List[str]]) -> int:
if not grid: return 0
self.grid = grid
count = 0
self.m, self.n = len(grid), len(grid[0])
for i in range(self.m):
for j in range(self.n):
if grid[i][j] == '1':
self.helper(i, j)
count+=1
return count
def helper(self, i, j):
self.grid[i][j] = '0'
for dx, dy in ((0, 1), (1, 0), (0, -1), (-1, 0)):
x = i + dx
y = j + dy
if 0 <= x < self.m and 0 <= y < self.n and self.grid[x][y] == '1':
self.helper(x, y)305 Number of Islands II
A 2d grid initials with zeroes. Given a list of positions to operate, count the number of islands after each addLand operation. Example:
Input: m = 3, n = 3, positions = [[0,0], [0,1], [1,2], [2,1]] Output: [1,1,2,3]
Use UnionFind, no group in the beginning, init all father node as -1
Check if neighbor has initiated(father[x]>0)
694. Number of Distinct Islands
Given a non-empty 2D array grid of 0's and 1's,Count the number of distinct islands. An island is considered to be the same as another if and only if one island can be translated (and not rotated or reflected) to equal the other.
Shift the nodes to original trigger point
Use Set to compare the islands
463. Island Perimeter
Only has 1 island
Image Matching
Image Matching 2
Last updated
Was this helpful?