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. Linux 查看系统相关信息(系统发型版本及内核版本等)

    以下为 Linux 以下信息查看命令,记录以备用 # uname -a # 查看内核/操作系统/CPU信息 # /etc/issue # 查看操作系统版本 # cat /proc/cpuinfo # ...

  2. 深入Spring Boot:那些注入不了的 Spring 占位符 ( ${} 表达式 )

    Spring里的占位符 spring里的占位符通常表现的形式是: 1 2 3 <bean id="dataSource" destroy-method="close ...

  3. java.lang.IllegalArgumentException: id to load is required for loading

    java.lang.IllegalArgumentException: id to load is required for loading at org.hibernate.event.LoadEv ...

  4. eclipse中web项目tomcat的设置

    1.  出现的问题: web开发中(eclipse环境),为本地项目添加tomcat,我们一般都会选择直接添加.在本次开发中突然遇到一个问题:因为项目涉及到文件上传,我利用MultipartFile进 ...

  5. BZOJ 4999 LCA树状数组差分维护DFS序

    Description 给一颗树,每个节点有个初始值 现在支持以下两种操作: 1. C i x(0<=x<2^31) 表示将i节点的值改为x 2. Q i j x(0<=x<2 ...

  6. 清北学堂清华大学钟皓曦神仙讲课day3摘要

    ---恢复内容开始--- 今天全是DP awsl,真的好难 先从斐波那契开始: dp:满足有一个状态边界条件(f[0]=0,f[1]=1) 边界条件:不需要计算其他状态的值而可以直接得出的状态或者最底 ...

  7. 上传大文件(100G)的解决方案

    4GB以上超大文件上传和断点续传服务器的实现 随着视频网站和大数据应用的普及,特别是高清视频和4K视频应用的到来,超大文件上传已经成为了日常的基础应用需求. 但是在很多情况下,平台运营方并没有大文件上 ...

  8. 【HDU1011】Starship Troopers

    题目大意:给定一棵 N 个节点的无根树,每个节点有一个重量和一个价值,现给出一些单位,每个单位可以接受 20 个重量单位,求如何分配这些单位,使得获得的价值最大. 题解:dp 好题qwq..真的毒瘤. ...

  9. Shiro(二)

    1 Shiro的授权 1.1 授权流程 1.2 授权方式 shiro支持三种方式的授权 1.2.1 编程式 通过编写if/else完成授权代码. if(subject.hasRole("ro ...

  10. day2计算机基础作业题

    1.什么是编程? 编程就是程序员用某种编程语言的语法格式将将自己脑中想让计算机做的事情写成文件.所以编程的结果就是一堆的文件,一堆文件就是的程序. 2.计算机的组成: 1).CPU 其中CPU又分为控 ...