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 ...
随机推荐
- Spherical Harmonics Lighting
[转自:http://www.cnblogs.com/daniagger/archive/2012/05/29/2524133.html] 1.背景知识 1.1 光照表示 之前我们都只考虑光源点和物体 ...
- Swift3.0 控制流
常用的一些判断方式 //for in let numberList = [, , , , ] //获取第一个元素 !拆包 print(numberList.first!)//1 //获取最后一个元素 ...
- csacademy Round #36(模拟+最坏情况)
传送门 题意 给出n种袜子,每种袜子个数a[i],两只相同种类袜子配成一对,询问至少拿出多少只袜子能确保配出k对袜子 分析 In order to find out the minimum numbe ...
- centos走一波
Cpu 作为一个具有特定功能的芯片,里面含有微指令集 如果你想让主机进行什么特异的运算,就需要参考Cpu是否有相关内置的微指令集 才可以由于Cpu的工作主要在于 管理和运算 ,因此Cpu内又可以分为两 ...
- 2018 年度码云热门项目排行榜 TOP 10
2016 年度码云热门项目排行榜 TOP 10 是通过开源项目2016年在码云上的 Watch.Star.Fork 数量来评定的榜单.码云平台发展至今,涌现了越来越多优秀的开源项目,越来越多的开源作者 ...
- 洛谷p1955[NOI2015]程序自动分析
题目: 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2,x3...代表程序中出现的变量,给定n个形如xi=xj或xi≠xj的变量 ...
- UVa 1218 Perfect Service 完美的服务
***状态设计值得一看dp[u][0]表示u是服务器(以下v均指任意u的子结点,son指u的所有子结点)ap[u][0]=sum{dp[v][1]}+1//错误,服务器是可以和其他服务器相邻的dp[u ...
- Asp.net MVC + Vue.js
@{ Layout = null; } <!DOCTYPE html><html> <head> <meta charset="UTF-8" ...
- 类成员的指针必须NULL化,否则是乱七八糟的东西
class BiTree { public: BiTree(); virtual ~BiTree(); virtual void insertNode(Node * newNode); virtual ...
- pandas中loc-iloc-ix的使用
转自:https://www.jianshu.com/p/d6a9845a0a34 Pandas中loc,iloc,ix的使用 使用 iloc 从DataFrame中筛选数据 iloc 是基于“位置” ...