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. 设置SVC模式

    清0:bic 置1:orr 访问cpsr和spdr要用到mrs和msr指令 mrs是把状态寄存器的值赋给通用寄存器 msr是把通用寄存器的值赋给状态寄存器 .text .global _start _ ...

  2. AIX中的进程管理

    1.AIX中的进程 (1)后台进程 后台进程运行时,用户不必等待当前后台进程的结束,即可以运行下一个进程. 后台进程的运行方式: # command &   (2)提高进程优先等级 -- ni ...

  3. CodeForces - 841D Leha and another game about graph

    给出一个连通图,并给每个点赋一个d值0或1或-1,要求选出一个边的集合,使得所有的点i要么d[i] == -1,要么 dgree[i] % 2 == d[i],dgree[i]代表i结点的度数. 考虑 ...

  4. Java线程与Linux内核线程的映射关系(转)

    Java线程与Linux内核线程的映射关系 Java线程与Linux内核线程的映射关系Linux从内核2.6开始使用NPTL (Native POSIX Thread Library)支持,但这时线程 ...

  5. JVM体系结构及优化

    源文档:https://docs.oracle.com/javase/8/docs/technotes/guides/vm/index.html JVM体系结构     方法区,类加载器,堆,Java ...

  6. 【LuoguP5206】[WC2019] 数树

    题目链接 题意 定义 \(F(T_1,T_2)=y^{n-common}\) 其中 \(common\) 为两棵树 \(T_1,T_2\) 的公共边条数. 三种问题 1.给定 \(T_1,T_2\) ...

  7. 解决JavaServer Faces 2.2 requires Dynamic Web Module 2.5 or newer问题

    ** 错误1: **在eclipse中新创建一个web项目的时候项目下的JSP文件中会爆出错误:The superclass “javax.servlet.http.HttpServlet” was ...

  8. CSS水印“点击穿透”

  9. 【java工具类】对字节数组字符串进行Base64解码并生成图片

    import java.io.File;import java.io.FileOutputStream;import java.io.OutputStream;import org.springfra ...

  10. Java+超大文件上传

    之前仿造uploadify写了一个HTML5版的文件上传插件,没看过的朋友可以点此先看一下~得到了不少朋友的好评,我自己也用在了项目中,不论是用户头像上传,还是各种媒体文件的上传,以及各种个性的业务需 ...