-
Notifications
You must be signed in to change notification settings - Fork 34
/
Solution.py
34 lines (33 loc) · 922 Bytes
/
Solution.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
#coding=utf-8
__author__ = 'xuxuan'
class Solution(object):
_landform=[]
_row=0
_col=0
def numIslands(self, grid):
"""
:type grid: List[List[str]]
:rtype: int
"""
self._landform=[]
self._row=len(grid)
if self._row==0:
return 0
for s in grid:
l=list(s)
self._landform.append(l)
self._col=len(grid[0])
ans=0
for x in range(self._row):
for y in range(self._col):
if self._landform[x][y]=='1':
ans+=1
self.explore(x,y)
return ans
def explore(self,x,y):
if x>=0 and x<self._row and y>=0 and y<self._col and self._landform[x][y]=='1':
self._landform[x][y]='2'
self.explore(x-1,y)
self.explore(x+1,y)
self.explore(x,y-1)
self.explore(x,y+1)