[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) 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,1,0,0,0,0,1,0,0,0,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0],
[0,1,1,0,1,0,0,0,0,0,0,0,0],
[0,1,0,0,1,1,0,0,1,0,1,0,0],
[0,1,0,0,1,1,0,0,1,1,1,0,0],
[0,0,0,0,0,0,0,0,0,0,1,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0],
[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:
[[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的更多相关文章
- 【LeetCode】695. Max Area of Island 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...
- 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 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@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 ...
- [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 ...
- leetcode 695 Max Area of Island 岛的最大面积
这个题使用深度优先搜索就可以直接遍历 DFS递归方法: class Solution { public: vector<vector<,},{,-},{,},{,}}; int maxAr ...
- 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 ...
- 【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 ...
随机推荐
- XMLHttpRequest对象的属性与方法
XMLHttpRequest对象是Ajax的核心,它有很多属性和方法.1,readyState属性当一个XMLHttpRequest对象被创立后,readyState属性标示了当前对象处于什么状态,可 ...
- 【算法随记三】小半径中值模糊的急速实现(16MB图7.5ms实现) + Photoshop中蒙尘和划痕算法解读。
在本人的博客里,分享了有关中值模糊的O(1)算法,详见:任意半径中值滤波(扩展至百分比滤波器)O(1)时间复杂度算法的原理.实现及效果 ,这里的算法的执行时间和参数是无关的.整体来说,虽然速度也很快, ...
- Spring Schema扩展机制
1:概述 Spring2.0开始,Spring提供XML Schema可扩展机制,用户可以自定义XML Schema文件,并自定义 XML Bean解析器,集成到Spring IOC容器中. 2:步骤 ...
- 用java打印日历
来自<java核心技术卷一> /** * Created by wangbin10 on 2019/1/3. * 打印当月日历 */ public class CalendarTest { ...
- java之继承中的静态变量
package Test; /** * Created by wangbin10 on 2018/7/9. * 我们知道静态变量属于类级别变量,对应每个类只有一份,类的所有实例共有一份,而成员变量则分 ...
- 美好生活从java开始
小编将会在接下来的日子里不断更新.分享一些IT方面的技术,以及自己的一些心得体会,希望大家能在我这有所收获.有所成长,那么我们就从java开始. 我们要想学习一样东西并且学好它,首先我们要弄清楚我们将 ...
- CentOS7.x mini安装OVS
命令均在root用户下运行: 一.关闭防护墙及selinux sed -i '/SELINUX/s/enforcing/disabled/g' /etc/selinux/config setenfor ...
- java获取系统类型与版本
System的properties中有很多系统属性: System.out.println(System.getProperty("os.name")); System.out.p ...
- JAVA复习笔记02
16.interface中的成员变量默认为public static final类型,方法只能是public(默认为public) 17.内部类访问外部类成员: Outer.this.num; 18. ...
- kali Linux渗透测试技术详解
kali Linux渗透测试技术详解 下载:https://pan.baidu.com/s/1g7dTFfzFRtPDmMiEsrZDkQ 提取码:p23d <Kali Linux渗透测试技术详 ...