leetcode-mid-Linked list- 200. Number of Islands¶
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¶的更多相关文章
- 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 用了第一种方式, ...
- 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 ...
- [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 ...
- [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 ...
- 【LeetCode】200. Number of Islands 岛屿数量
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- 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 ...
- [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 ...
- (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 ...
- 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 ...
- 【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. ...
随机推荐
- luogu P3210 [HNOI2010]取石头游戏
传送门 不会结论做个鬼系列 题意其实是在头尾(最多)两个栈以及中间一些双端队列依次取数,然后每个人都要最大化自己的价值 有一个结论,如果一段序列中,出现了三个相邻位置\(A,B,C\),满足\(A\l ...
- git 计算commit
转自:https://www.cnblogs.com/jwentest/p/8186712.html#!/bin/bash f1="$1*" f2="$2*" ...
- Python基础——函数的迭代器和生成器
等待更新…………………… 后面再写
- redis常见问题和解决方案
转载:https://www.cnblogs.com/aspirant/p/6820262.html [原创]那些年用过的Redis集群架构(含面试解析) redis常见问题和解决方案 持久化.主从问 ...
- TCP Retransmission 连接超时
TCP Retransmission 连接超时 kame 2019/3/17 33 TCP 记一次TCP 连接超时 背景 用户反馈 >> 有出现支付超时.页面问题 (部分情况会出现) 分析 ...
- linux各路径(目录)的解释
目录 /bin 存放二进制可执行文件(ls,cat,mkdir等),常用命令一般都在这里. /etc 存放系统管理和配置文件 /home 存放所有用户文件的根目录,是用户主目录的基点, 比如用户use ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(1)|开篇]
今天我们来开一个新系列. 从零学习rustlang. 简单介绍下rustlang: Rust 是一门系统级编程语言,被设计为保证内存和线程安全,防止段错误产生.作为系统级编程语言,它的基本理念是 “零 ...
- Bootstarp-table入门(1)
https://blog.csdn.net/dlf123321/article/details/52231926?locationNum=11&fps=1
- 【HDU4003】Find Metal Mineral
题目大意:给定一棵 N 个节点的有根树,边有边权,在根结点处有 K 个人,这些人会遍历树上的所有边,求如何遍历才能使得所有人走过路径的边权和最小. 题解: 引理:对于一棵子树来说,若存在 M>0 ...
- Java中InputStream和String之间的转换方法
1.InputStream转化为String1.1 JDK原生提供方法一:byte[] bytes = new byte[0];bytes = new byte[inputStream.availab ...