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:

  1. [[0,0,1,0,0,0,0,1,0,0,0,0,0],
  2. [0,0,0,0,0,0,0,1,1,1,0,0,0],
  3. [0,1,1,0,1,0,0,0,0,0,0,0,0],
  4. [0,1,0,0,1,1,0,0,1,0,1,0,0],
  5. [0,1,0,0,1,1,0,0,1,1,1,0,0],
  6. [0,0,0,0,0,0,0,0,0,0,1,0,0],
  7. [0,0,0,0,0,0,0,1,1,1,0,0,0],
  8. [0,0,0,0,0,0,0,1,1,0,0,0,0]]

Given the above grid, return 6. Note the answer is not 11, because the island must be connected 4-directionally.

Example 2:

  1. [[0,0,0,0,0,0,0,0]]

Given the above grid, return 0.

Note: The length of each dimension in the given grid does not exceed 50.

在矩阵中找相连起来的最大的1s串(上下左右) 并计数返回

自己写的dfs:

def finded(x, y, nums):
ans2 = 1
if nums[x][y] == 0:
return 0
nums[x][y] = 0
if x - 1 >= 0:
ans2 += finded(x - 1, y, nums)
if x + 1 < len(nums):
ans2 += finded(x + 1, y, nums)
if y + 1 < len(nums[0]):
ans2 += finded(x, y + 1, nums)
if y - 1 >= 0:
ans2 += finded(x, y - 1, nums)
return ans2

class Solution:
def findMaxConsecutiveOnes(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
r = len(nums)
c = len(nums[0])
ans = 0
for i in range(r):
for j in range(c):
if nums[i][j] == 0:
continue
res = finded(i, j, nums)
if ans < res:
ans = res
return ans

题解中的DfS算法:

class Solution(object):
def maxAreaOfIsland(self, grid):
seen = set()

def area(r, c):
if not (0 <= r < len(grid) and 0 <= c < len(grid[0])
and (r, c) not in seen and grid[r][c]):
return 0
seen.add((r, c))
return (1 + area(r + 1, c) + area(r - 1, c) +
area(r, c - 1) + area(r, c + 1))

return max(area(r, c)
for r in range(len(grid))
for c in range(len(grid[0])))

[leetcode]python 695. Max Area of Island的更多相关文章

  1. 【LeetCode】695. Max Area of Island 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...

  2. 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 用了第一种方式, ...

  3. 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 ...

  4. 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) conn ...

  5. [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 ...

  6. leetcode 695 Max Area of Island 岛的最大面积

    这个题使用深度优先搜索就可以直接遍历 DFS递归方法: class Solution { public: vector<vector<,},{,-},{,},{,}}; int maxAr ...

  7. 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 ...

  8. 【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) ...

  9. 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 ...

随机推荐

  1. QT4与QT3的不同

    1)QT4中提供了大量新控件,虽然它也保持了旧的控件,并命名为Qt3XXX,但是这样的控件没准在今后的哪个QT版本中就不被支持了,所以还是换吧,控件替换的工作是巨大的,这些新的控件使用了新的方法.属性 ...

  2. 原创powershell脚本:通过远程桌面3389黑名单,阻止黑客ip

    远程桌面 3389 ban ip 防火墙 rdp   通过远程桌面3389黑名单,阻止黑客ip.这是一个常见的需求.但我搜遍了谷歌也找不到成品脚本.想做搬运工却做不成,只能自己费尽写.下载备用吧,估计 ...

  3. HTML连载11-HTML中被废弃的标签&字符实体

    ​一.为什么会有被废弃的标签 答:HTML中以前存在一部分不是用来添加语义的标签,而与我们HTML标签是用来添加语义的,这与我们的定义不相符. 例如: 1.标签<br>:换行 2.标签&l ...

  4. 如何开发优质的 Flutter App:应用架构的搭建

    各位读者朋友们,好久不见了! 最近博主一直在忙于工作以及写<Flutter入门与应用实战>的书,所以没有时间打理博客.今天来给大家分享一个博主在GitChat上发起的一场Chat. 下面是 ...

  5. 基于 ZooKeeper 搭建 Spark 高可用集群

    一.集群规划 二.前置条件 三.Spark集群搭建         3.1 下载解压         3.2 配置环境变量         3.3 集群配置         3.4 安装包分发 四.启 ...

  6. 参数传递机制之JWT

    1. 什么是 JWT JWT 其全称为:JSON Web Token,简单地说就是 JSON 在 Web 上的一种带签名的标记形式.官方的定义如下: JSON Web Tokens are an op ...

  7. ABP开发框架前后端开发系列---(13)高级查询功能及界面的处理

    在一般的检索界面中,基于界面易用和美观方便的考虑,我们往往只提供一些常用的条件查询进行列表数据的查询,但是有时候一些业务表字段很多,一些不常见的条件可能在某些场景下也需要用到.因此我们在通用的查询条件 ...

  8. ASP.NET Core on K8S学习初探(2)K8S基本概念快速一览

    在上一篇<单节点环境搭建>中,通过Docker for Windows在Windows开发机中搭建了一个单节点的K8S环境,接下来就是动人心弦的部署ASP.NET Core API到K8S ...

  9. 如何在VMware12中安装centos6.7系统

    一.安装虚拟机,步骤如下: 1.安装好VMware12软件(略过),安装完后点击创建新的虚拟机 2.选择自定义类型安装 3.点击下一步 4.选择稍后安装操作系统,点击[下一步]. 5.客户机操作系统选 ...

  10. 用 IQ分布模拟图来测试浏览器的性能

    今天天气太凉快,跟这个日历上属于夏天的那一页显得格格不入!就连我我床下那台废弃的ThinkPad,居然也十分透凉气,那外壳连我的体温高都没有,于是,我就开始想一个方法,让我那个废弃的电脑发热,顺便用它 ...