-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path200.py
71 lines (65 loc) · 2.14 KB
/
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'''
200. Number of Islands
https://leetcode.com/problems/number-of-islands/
Hi, here's your problem today. This problem was recently asked by LinkedIn:
Given a 2-dimensional grid consisting of 1's (land blocks) and 0's (water blocks), count the number of islands present in the grid. The definition of an island is as follows:
1.) Must be surrounded by water blocks.
2.) Consists of land blocks (1's) connected to adjacent land blocks (either vertically or horizontally).
Assume all edges outside of the grid are water.
Example:
Input:
10001
11000
10110
00000
Output: 3
'''
from typing import *
class Solution:
def inRange(self, grid, j, i):
numRow, numCol = len(grid), len(grid[0])
if j < 0 or i < 0 or j >= numRow or i >= numCol:
return False
return True
def dfsVisit(self, grid, visited, j, i):
if not self.inRange(grid, j, i) or \
grid[j][i]!="1" or visited[j][i]:
return
visited[j][i] = True
self.dfsVisit(grid, visited, j-1, i)
self.dfsVisit(grid, visited, j, i-1)
self.dfsVisit(grid, visited, j+1, i)
self.dfsVisit(grid, visited, j, i+1)
def numIslands(self, grid: List[List[str]]) -> int:
count = 0
if grid and grid[0]:
visited = [[False]*len(grid[0]) for _ in range(len(grid))]
for j in range(len(grid)):
for i in range(len(grid[j])):
if grid[j][i]=="1" and not visited[j][i]:
self.dfsVisit(grid, visited, j-1, i)
self.dfsVisit(grid, visited, j, i-1)
self.dfsVisit(grid, visited, j+1, i)
self.dfsVisit(grid, visited, j, i+1)
count += 1
return count
print(Solution().numIslands([
["1","1","1","1","0"],
["1","1","0","1","0"],
["1","1","0","0","0"],
["0","0","0","0","0"],
]))
#1
print(Solution().numIslands([
["1","1","0","0","0"],
["1","1","0","0","0"],
["0","0","1","0","0"],
["0","0","0","1","1"],
]))
#3
print(Solution().numIslands([
["1","1","1"],
["0","1","0"],
["1","1","1"]
]))
#1