mycode  57.92%

class Solution(object):
def numIslands(self, grid):
"""
:type grid: List[List[str]]
:rtype: int
"""
def recursive(i,j,row,col):
if i>=0 and i<row and j>=0 and j<col:
if grid[i][j] == '':
return
grid[i][j] = ''
if i-1 >= 0:
recursive(i-1,j,row,col)
if i+1 < row:
recursive(i+1,j,row,col)
if j-1 >= 0:
recursive(i,j-1,row,col)
if j+1 <= col:
recursive(i,j+1,row,col)
else:
return
if not grid:
return 0
row = len(grid)
col = len(grid[0])
for i in range(row):
for j in range(col):
if grid[i][j] == '':
#print('if..',grid)
recursive(i,j,row,col)
grid[i][j] = ''
#print('if..',grid)
count = 0
for i in range(row):
for j in range(col):
if grid[i][j] == '':
count += 1
return count

参考:

思路:其实第二次双层for循环是多余的,可以在前面就计数

class Solution:
def numIslands(self, grid):
"""
:type grid: List[List[str]]
:rtype: int
"""
res = 0
for r in range(len(grid)):
for c in range(len(grid[0])):
if grid[r][c] == "":
self.dfs(grid, r, c)
res += 1
return res def dfs(self, grid, i, j):
dirs = [[-1, 0], [0, 1], [0, -1], [1, 0]]
grid[i][j] = ""
for dir in dirs:
nr, nc = i + dir[0], j + dir[1]
if nr >= 0 and nc >= 0 and nr < len(grid) and nc < len(grid[0]):
if grid[nr][nc] == "":
self.dfs(grid, nr, nc)

leetcode-mid-Linked list- 200. Number of Islands¶的更多相关文章

  1. leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions

    两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...

  2. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  3. [leetcode]200. Number of Islands岛屿个数

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  4. [LeetCode] 200. Number of Islands 岛屿的数量

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  5. 【LeetCode】200. Number of Islands 岛屿数量

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  6. Leetcode 200. number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  7. [LeetCode] 200. Number of Islands 解题思路

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  8. (BFS/DFS) leetcode 200. Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  9. leetcode题解 200. Number of Islands(其实就是一个深搜)

    题目: Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is s ...

  10. 【LeetCode】200. Number of Islands (2 solutions)

    Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. ...

随机推荐

  1. sql server 平方根函数SQRT(x)

    --SQRT(x)返回非负数x的二次方根 示例:select  SQRT(9), SQRT(36); 结果:3    6

  2. wex5 如何在js中给data添加数据

    var options = { defaultValues :[ {'xuetang' : xuetang,'time' : time} ] }; this.comp("xuetangDat ...

  3. oracle常用函数(1)

    oracle有很强大的函数功能,常用的字符处理函数如下: SQL> select initcap('hello') from dual;//将首字母转为大写 INITCAP('HELLO')-- ...

  4. mac osx sed 命令

    $ sed -i "s/devicedemo/device/g" `grep devicedemo -rl ./` sed: 1: ".//.coveragerc&quo ...

  5. TS学习

    随着vue3.0的即将到来,是时候学习一下TS了 简介:TypeScript是一种由微软开发的自由和开源的编程语言.它是JavaScript的一个超集,而且本质上向这个语言添加了可选的静态类型和基于类 ...

  6. Spring boot 拦截器和过滤器

    1. 过滤器 Filter介绍 Filter可以认为是Servlet的一种“加强版”,是对Servlet的扩展(既可以对请求进行预处理,又可以对处理结果进行后续处理.使用Filter完整的一般流程是: ...

  7. Spring Framework Part3 IoC and Dynamic Proxy

    spring serious of blog edit by 马士兵教育 Maven方式创建Spring工程 工程创建 1.新建项目 选择Maven Project 2.勾选 Create a sim ...

  8. java面试(反射)05

    1.什么是反射 JAVA反射机制是在运行状态中,对于任意一个类,都能够获取这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取类信息以及动态调用对象内容就称为jav ...

  9. 安装mysql的步骤并利用mysql原始密码修改自定义密码

    1.给刚下载好的mysql软件tar包,进行解包 命令:tar -xf mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar 然后利用yum装包 命令:yum -y ins ...

  10. DirectX屏幕捕获和输出视频

    #include <Windows.h> #include <mfapi.h> #include <mfidl.h> #include <Mfreadwrit ...