[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) 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.
思路:深度优先搜索。从第一行第一个开始遍历整个数组,如果某个位置的值是 1 ,那么就开始深度优先搜索。
在深度优先搜索中,我们先要看看与该位置连通有几种可能的情况,很明显是4种,即上下左右。所以我们得用
个循环来遍历这四种可能的情况。如果上或下或左或右的值是1,我们就用递归函数递归上或下或左或右的位置,
求解他们连通的情况。
class Solution {
private int sum = 0,maxSum = 0;
public int maxAreaOfIsland(int[][] grid) {
for (int i=0;i<grid.length;i++){
for (int j=0;j<grid[i].length;j++){
if (grid[i][j]==1){
sum = 0;
dfs(grid,i,j);
}
}
}
return maxSum;
}
private void dfs(int[][] grid,int i,int j){
sum +=grid[i][j];
grid[i][j]= 0;
if (i-1>=0&&grid[i-1][j]==1)
dfs(grid,i-1,j);
if (i+1<grid.length&&grid[i+1][j]==1)
dfs(grid,i+1,j);
if (j-1>=0&&grid[i][j-1]==1)
dfs(grid,i,j-1);
if (j+1<grid[i].length&&grid[i][j+1]==1)
dfs(grid,i,j+1);
if (sum>maxSum){
maxSum = sum;
} }
}
[Leetcode]695. Max Area of Island的更多相关文章
- 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 ...
- 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】Max Area of Island
国庆中秋长假过完,又要开始上班啦.先刷个题目找找工作状态. Given a non-empty 2D array grid of 0's and 1's, an island is a group o ...
- 【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 ...
- 【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 ...
随机推荐
- Pandas 0 数据结构Series
# -*- encoding:utf-8 -*- # Copyright (c) 2015 Shiye Inc. # All rights reserved. # # Author: ldq < ...
- [Tips]vim设置
临时设置 在vim中输入 :set nu! 若显示行号时,它的功能时取消行号:若不显示行号时,它的功能是显示行号. 固定设置 在~/.vimrc中进行设置. 添加注释: 双引号是注释 ” this i ...
- github-新建文件夹
1,进入仓库“ sstruggle.github.io ”中,在该仓库页面中找到“ Create new file ”,如图: 2,在创建新文件页面,输入“ js/ ”,github默认为是一个文件夹 ...
- BZOJ 2169
$f_{ij}$ 表示加入 $i$ 条边, $j$ 个点的度数是奇数的方案数,然后暴力 #include<bits/stdc++.h> using namespace std; #defi ...
- vue 实现图片上传与预览,以及清除图片
vue写原生的上传图片并预览以及清除图片的效果,下面是demo,其中里面有vue获取input框value值的方法以及vue中函数之间的调用 <!DOCTYPE html> <htm ...
- vue开发(开发环境+项目搭建)
Vue.js是一套构建用户界面的渐进式框架.与其他重量级框架不同的是,Vue 采用自底向上增量开发的设计.Vue 的核心库只关注视图层,并且非常容易学习,非常容易与其它库或已有项目整合.另一方面,Vu ...
- 纯css修改单选、复选按钮样式
只支持IE9及以上 html <label><input class="radio" type="radio" name="radi ...
- Reveal : Xcode辅助界面调试工具
Reveal简介: Reveal是一款iOS界面调试工具,辅助Xcode进行界面调试,使用它可以在iOS开发的时候动态的查看和修改应用程序的界面. 软件下载 首先去官网下载Reveal,下载地址:ht ...
- 有关promise的技巧
其实promise的作用是将异步的代码转化为同步,这里的异步指的是request1,request2.
- 依赖注入[8]: .NET Core DI框架[服务消费]
包含服务注册信息的IServiceCollection对象最终被用来创建作为DI容器的IServiceProvider对象.当需要消费某个服务实例的时候,我们只需要指定服务类型调用IServicePr ...