695. Max Area of Island@python
Given a non-empty 2D array grid
of 0's and 1's, an island is a group of 1
's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.
Find the maximum area of an island in the given 2D array. (If there is no island, the maximum area is 0.)
Example 1:
[[0,0,,0,0,0,0,,0,0,0,0,0],
[0,0,0,0,0,0,0,,,,0,0,0],
[0,,,0,,0,0,0,0,0,0,0,0],
[0,,0,0,,,0,0,,0,,0,0],
[0,,0,0,,,0,0,,,,0,0],
[0,0,0,0,0,0,0,0,0,0,,0,0],
[0,0,0,0,0,0,0,,,,0,0,0],
[0,0,0,0,0,0,0,,,0,0,0,0]]
Given the above grid, return 6
. Note the answer is not 11, because the island must be connected 4-directionally.
题意:在一个二维数组中,找出能够连接在一起的数字1的最大长度
思路:DFS
代码:
class Solution(object):
def maxAreaOfIsland(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
res = 0
m = len(grid)
n = len(grid[0])
for i in range(m): # 遍历数组
for j in range(n):
if grid[i][j] == 1:
res = max(res, self.dfs(grid, i, j))
return res def dfs(self, grid, i, j):
m = len(grid)
n = len(grid[0])
if i < 0 or i > m-1 or j < 0 or j > n-1 \
or grid[i][j] == 0:
return 0
count = 1
grid[i][j] = 0 # 将值变为0,防止重复计数或者递归栈溢出
count += self.dfs(grid, i+1, j) + \
self.dfs(grid, i-1, j) + \
self.dfs(grid, i, j-1) + \
self.dfs(grid, i, j+1) return count
时间复杂度: O(mn)
空间复杂度:O(1)
695. Max Area of Island@python的更多相关文章
- 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 用了第一种方式, ...
- [leetcode]python 695. Max Area of Island
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...
- 【LeetCode】695. Max Area of Island 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...
- 200. Number of Islands + 695. Max Area of Island
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- LeetCode 695. Max Area of Island (岛的最大区域)
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...
- 【easy】695. Max Area of Island
题目: Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) ...
- 695. Max Area of Island最大岛屿面积
[抄题]: 求最多的联通的1的数量 Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (repre ...
- [Leetcode]695. Max Area of Island
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...
- 695. Max Area of Island
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
随机推荐
- E20180405-hm
tutorial n. 个别辅导时间; 教程,辅导材料; 使用说明书; 辅导课; adj. 导师的; 私人教师的; 辅导的; track n. 小路,小道; 痕迹,踪迹; 轨道,音轨; 方针,路线 ...
- 【POJ - 3190 】Stall Reservations(贪心+优先队列)
Stall Reservations 原文是English,这里直接上中文吧 Descriptions: 这里有N只 (1 <= N <= 50,000) 挑剔的奶牛! 他们如此挑剔以致于 ...
- 黑客攻防技术宝典web实战篇:利用信息泄露习题
猫宁!!! 参考链接:http://www.ituring.com.cn/book/885 随书答案. 1. 当探查 SQL 注入漏洞时,如果请求以下 URL:https://wahh-app.com ...
- 黑客攻防技术宝典web实战篇:定制攻击自动化习题
猫宁!!! 参考链接:http://www.ituring.com.cn/book/885 随书答案. 1. 指出使用自动技巧在应用程序中枚举标识符时用到的 3 个标识符“触点”. (a) HTTP ...
- jQuery笔记之热点搜索排名小demo
先来看一下成品图: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- Hue的全局配置文件hue.ini(图文详解)
Hue版本:hue-3.9.0-cdh5.5.4 需要编译才能使用(联网) 说给大家的话:大家电脑的配置好的话,一定要安装cloudera manager.毕竟是一家人的.同时,我也亲身经历过,会有部 ...
- h5-17-元素拖放
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- list的一些功能
x = [1,5,2,3,4] 1.列表反转序: 函数法: x.reverse()该方法没有返回值但会对列表进行反向排序. 注意 不能y=x.reverse(),会得到None 如果要的话要y=rev ...
- CentOS 6.9:MySQL Daemon failed to start.
[root@Server_1 12:02:58 ~ 25]#service mysqld start MySQL Daemon failed to start.Starting mysqld: [ro ...
- JVM 内存机制理解【转自http://www.cnblogs.com/dingyingsi/p/3760447.html】
我们知道,计算机CPU和内存的交互是最频繁的,内存是我们的高速缓存区,用户磁盘和CPU的交互,而CPU运转速度越来越快,磁盘远远跟不上CPU的读写速度,才设计了内存,用户缓冲用户IO等待导致CPU的等 ...