问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3736 访问。

给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合。你可以假设二维矩阵的四个边缘都被水包围着。

找到给定的二维数组中最大的岛屿面积。(如果没有岛屿,则返回面积为0。)

[[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]]

对于上面这个给定矩阵应返回 6。注意答案不应该是11,因为岛屿只能包含水平或垂直的四个方向的‘1’。

注:以上紫色部分为该题的解,粉红色部分是要注意的部分,答案是6而不是11。

[[0,0,0,0,0,0,0,0]]

对于上面这个给定的矩阵, 返回 0。

注意: 给定的矩阵grid 的长度和宽度都不超过 50。


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

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

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


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3736 访问。

public enum SearchType {
DepthFirstSearch,
BreadthFirstSearch
} public class Program { public static void Main(string[] args) {
int[,] nums = null; nums = new int[,]{
{ 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}
}; var res = MaxAreaOfIsland(nums, SearchType.DepthFirstSearch);
Console.WriteLine(res); nums = new int[,]{
{ 0,0,0,0,0,0,0,0}
}; res = MaxAreaOfIsland(nums, SearchType.BreadthFirstSearch);
Console.WriteLine(res); Console.ReadKey();
} private static int MaxAreaOfIsland(int[,] grid, SearchType searchType) {
//记录最大值
var max = 0;
//记录行和列数量
var rows = grid.GetLength(0);
var cols = grid.GetLength(1);
//标记是否已经访问过了
var visited = new bool[rows, cols];
if(searchType == SearchType.DepthFirstSearch) {
//使用深度优先搜索来遍历原数组
for(var i = 0; i < rows; i++) {
for(var j = 0; j < cols; j++) {
if(visited[i, j]) continue;
max = Math.Max(max, DepthFirstSearch(grid, i, j, visited));
}
}
} else {
max = Math.Max(max, BreadthFirstSearch(grid, visited));
}
//返回最大值
return max;
} private static int DepthFirstSearch(int[,] grid, int i, int j, bool[,] visited) {
//深度优先搜索
//记录行和列数量
var rows = grid.GetLength(0);
var cols = grid.GetLength(1);
//处理边界
if(i >= rows || j >= cols || i < 0 || j < 0) return 0;
//若已经访问或不是岛屿则返回0
if(visited[i, j] || grid[i, j] == 0) return 0;
//标记当前节点已经访问过了
visited[i, j] = true;
//流程到了这里,当前节点为岛屿,所以返回上、下、左、右4个方向的结果+1
return DepthFirstSearch(grid, i + 1, j, visited) +
DepthFirstSearch(grid, i - 1, j, visited) +
DepthFirstSearch(grid, i, j + 1, visited) +
DepthFirstSearch(grid, i, j - 1, visited) + 1;
} private static int BreadthFirstSearch(int[,] grid, bool[,] visited) {
//广度优先搜索
//记录最大值
var max = 0;
//记录行和列数量
var rows = grid.GetLength(0);
var cols = grid.GetLength(1);
//开始搜索
for(var i = 0; i < rows; i++) {
for(var j = 0; j < cols; j++) {
//若已经访问或不是岛屿则返回0
if(visited[i, j] || grid[i, j] == 0) continue;
//记录临时岛屿面积
var area = 0;
//栈在广度优先中的应用
var stack = new Stack<KeyValuePair<int, int>>();
//将当前节点压入栈中
stack.Push(KeyValuePair.Create(i, j));
//标记当前节点已经访问过了
visited[i, j] = true;
//栈空时,代表当前广度(横向)已搜索完毕
while(stack.Count != 0) {
//临时岛屿+1
area++;
//取出栈顶数据并记录
var curNode = stack.Pop();
//记录刚取出的栈顶元素的索引值
var rowIndex = curNode.Key;
var colIndex = curNode.Value;
//从上、下、左、右4个方向
var fix = new int[4, 2] {
{-1,0 },
{ 1,0},
{ 0,-1},
{ 0,1}
};
for(int k = 0; k < fix.GetLength(0); k++) {
//处理边界
if(rowIndex + fix[k, 0] >= 0 &&
rowIndex + fix[k, 0] < rows &&
colIndex + fix[k, 1] >= 0 &&
colIndex + fix[k, 1] < cols) {
//若为1或没有访问过,则继续
if(grid[rowIndex + fix[k, 0], colIndex + fix[k, 1]] == 1 &&
visited[rowIndex + fix[k, 0], colIndex + fix[k, 1]] == false) {
stack.Push(KeyValuePair.Create(rowIndex + fix[k, 0], colIndex + fix[k, 1]));
visited[rowIndex + fix[k, 0], colIndex + fix[k, 1]] = true;
}
}
}
}
//比较当前最大值
max = Math.Max(max, area);
}
}
//返回最大值
return max;
} }

以上给出2种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3736 访问。

6
0

分析:

显而易见,深度优先搜索和广度优先搜索的时间复杂度均为:  。

C#LeetCode刷题之#695-岛屿的最大面积( Max Area of Island)的更多相关文章

  1. [Swift]LeetCode695. 岛屿的最大面积 | 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 ...

  2. C#LeetCode刷题-数组

    数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...

  3. C#LeetCode刷题-深度优先搜索

    深度优先搜索篇 # 题名 刷题 通过率 难度 98 验证二叉搜索树   22.2% 中等 99 恢复二叉搜索树   45.1% 困难 100 相同的树   48.1% 简单 101 对称二叉树   4 ...

  4. C#LeetCode刷题-哈希表

    哈希表篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 42.8% 简单 3 无重复字符的最长子串   24.2% 中等 18 四数之和   ...

  5. [LeetCode]695. 岛屿的最大面积(DFS/BFS)、200. 岛屿数量(DFS/BFS待做/并差集待做)

    695. 岛屿的最大面积 题目 给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合.你可以假设二维矩阵的四个边缘都被 ...

  6. leetcode刷题目录

    leetcode刷题目录 1. 两数之和 2. 两数相加 3. 无重复字符的最长子串 4. 寻找两个有序数组的中位数 5. 最长回文子串 6. Z 字形变换 7. 整数反转 8. 字符串转换整数 (a ...

  7. LeetCode刷题专栏第一篇--思维导图&时间安排

    昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...

  8. leetcode 刷题进展

    最近没发什么博客了 凑个数 我的leetcode刷题进展 https://gitee.com/def/leetcode_practice 个人以为 刷题在透不在多  前200的吃透了 足以应付非算法岗 ...

  9. LeetCode刷题指南(字符串)

    作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...

随机推荐

  1. GPO - General GPO Settings(1)

    Prohibit access to Control Panel and PC settings Disable GPO for Administrators and /or User Groups  ...

  2. javascript兼容问题 : let降级到var 遇到的闭包问题的解决方法

    我用  for(let x in y) 循环一个图片元素数组. for (let x in arr) { arr[x].onload = function(){ /* ... */ } } 这样写是没 ...

  3. 重磅分享:美团点评架构师私藏的内部Linux运维笔记

    最近不少小伙伴后台联系,希望能弄一些大厂的学习资料,我这边费了很大劲,联系到老朋友,原美团点评架构师张sir,问他要了些美团点评架构的内部资料. 这份资料含金量非常高,包含整个美团点评架构架构图,Li ...

  4. java消除整型数组中重复的元素,排序后输出新数组

    法一: import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(S ...

  5. 借鉴一个比较标准的后端RESTful API

    我们制定的 API 规范,使用了微服务架构所以做了一些改进,我们更偏向使用 http code 标识,不然需要自己处理成功或失败的逻辑,在 200 内再包一层显得啰嗦:并且微服务系列都不支持,Feig ...

  6. 全卷积神经网络FCN详解(附带Tensorflow详解代码实现)

    一.导论 在图像语义分割领域,困扰了计算机科学家很多年的一个问题则是我们如何才能将我们感兴趣的对象和不感兴趣的对象分别分割开来呢?比如我们有一只小猫的图片,怎样才能够通过计算机自己对图像进行识别达到将 ...

  7. awk格式化

    1,获取指定列和行,在指定列和行内插入 指定符号. /p/v2/api/winapi/mini/merchant/admin/notice/list/1/10?current=1&size=1 ...

  8. 什么是 A/B 测试?

    1.什么是A/B 测试?有什么用? 做过App功能设计的读者朋友可能经常会面临多个设计方案的选择,例如某个按钮是用蓝色还是黄色,是放左边还是放右边. 传统的解决方法通常是集体讨论表决,或者由某位专家或 ...

  9. centOS7.*安装nginx和简单使用

    安装nginx 去官网下载对应的nginx包,推荐使用稳定版本. 上传下载好的包到服务器 安装依赖环境 安装gcc环境. yum install gcc-c++ 安装PCRE库,用于解析正则表达式. ...

  10. PHP date_time_set() 函数

    ------------恢复内容开始------------ 实例 设置时间: <?php$date=date_create("2013-05-01");date_time_ ...