:type grid: List[List[str]] :rtype: int
(self, court)
| 37 | (x+1, y)) |
| 38 | |
| 39 | def numIslands(self, court): |
| 40 | """ |
| 41 | :type grid: List[List[str]] |
| 42 | :rtype: int |
| 43 | """ |
| 44 | fans_groups = [] |
| 45 | x = 0 |
| 46 | y = 0 |
| 47 | if not court: |
| 48 | return 0 |
| 49 | |
| 50 | x_length = len(court[0]) |
| 51 | y_length = len(court) |
| 52 | |
| 53 | def helper(x, y): |
| 54 | Xy = self.makeXY(x, y) |
| 55 | for i in Xy: |
| 56 | try: |
| 57 | if i[1] < 0 or i[0] < 0: |
| 58 | continue |
| 59 | |
| 60 | if court[i[1]][i[0]] == '1': |
| 61 | court[i[1]][i[0]] = '0' |
| 62 | t = helper(i[0], i[1]) |
| 63 | except IndexError: |
| 64 | continue |
| 65 | else: |
| 66 | return 1 |
| 67 | |
| 68 | |
| 69 | for y in range(y_length): |
| 70 | for x in range(x_length): |
| 71 | if court[y][x] == '1': |
| 72 | court[y][x] = '0' |
| 73 | fans_groups.append(helper(x, y)) |
| 74 | |
| 75 | |
| 76 | |
| 77 | if not fans_groups: |
| 78 | return 0 |
| 79 | |
| 80 | return len(fans_groups) |
| 81 |