本文主要是介绍LeetCode刷题:695. Max Area of Island(JAVA代码详解),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
LeetCode刷题:695. Max Area of Island
原题链接:https://leetcode.com/problems/max-area-of-island/description/
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.
这个题目的意思是给定了一个非空的二维数组,单元格的值由0和1构成。1代表岛(陆地),0代表水。岛是由上下左右相邻的陆地构成。
假设二维数组的四周边界之外都是水。
在给定的二维数组中找到岛(陆地)的最大面积。如果给定的二维数组中没有岛,则岛的最大面积为0。
问题分析
这个题目可以考虑从二维数组的左上角位置,其坐标为[0,0]开始进行遍历,采用DFS搜索算法,找出某一个位置,其上下左右相邻的位置均为1的最大数,即为岛的面积。
对于二维数组中某一个元素的位置 [i,j] 来说,搜索的方向有4个,分别确定其坐标进行搜索:
[ i + 1 , j ] , [ i , j + 1 ] , [ i − 1 , j ] , [ i , j − 1 ] [i+1,j],[i,j+1],[i-1,j],[i,j-1] [i+1,j],[i,j+1],[i−1,j],[i,j−1]
算法实现
编写一个方法maxAreaOfIsland()来求岛的最大面积,返回值即为所求的最大面积。
public int maxAreaOfIsland(int[][] grid) {int max = 0//......return max;
}
编写一个双重循环,从给定的二维数组的左上角,即[0,0]位置开始搜索,当满足条件 grid[i][j] == 1 时,调用DFS算法向其周围的四个方向进行搜索。代码如下:
public int maxAreaOfIsland(int[][] grid) {//如果grid为空或者grid的长度为0,则返回。结束。if (grid == null || grid.length == 0) {return 0;}//获得M*N矩阵的维度int m = grid.length;int n = grid[0].length;//设置max初始值为0int max = 0;//双重循环for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {//如果grid[i][j]的值为1,则进行搜索if (grid[i][j] == 1) {int area = dfs(grid, i, j, m, n, 0);//取最大值max = Math.max(area, max);}}}//返回最大值return max;}
DFS算法如何设计呢?
/** DFS搜索算法思路* 输入参数:* grid—矩阵* i,j 表示矩阵元素的坐标* m,n 表示矩阵的行和列* area表示最大面积* */int dfs(int[][] grid, int i, int j, int m, int n, int area) {if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == 0) {return area;}//标记为0,搜索过的单元格位置标记为0grid[i][j] = 0;//area加1area++;//继续向4个方向搜索area = dfs(grid, i + 1, j, m, n, area);area = dfs(grid, i, j + 1, m, n, area);area = dfs(grid, i - 1, j, m, n, area);area = dfs(grid, i, j - 1, m, n, area);//返回areareturn area;}
完整的算法实现
package com.bean.algorithmbasic;public class MaxAreaofIsland {public int maxAreaOfIsland(int[][] grid) {//如果grid为空或者grid的长度为0,则返回。结束。if (grid == null || grid.length == 0) {return 0;}//获得M*N矩阵的维度int m = grid.length;int n = grid[0].length;//设置max初始值为0int max = 0;//双重循环for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {//如果grid[i][j]的值为1,则进行搜索if (grid[i][j] == 1) {int area = dfs(grid, i, j, m, n, 0);//取最大值max = Math.max(area, max);}}}//返回最大值return max;}int dfs(int[][] grid, int i, int j, int m, int n, int area) {if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == 0) {return area;}//标记为0grid[i][j] = 0;//area加1area++;//继续向4个方向搜索area = dfs(grid, i + 1, j, m, n, area);area = dfs(grid, i, j + 1, m, n, area);area = dfs(grid, i - 1, j, m, n, area);area = dfs(grid, i, j - 1, m, n, area);//返回areareturn area;}public static void main(String args[]) {int[][] map= { {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}};MaxAreaofIsland maos=new MaxAreaofIsland();int ANSWER=maos.maxAreaOfIsland(map);System.out.println("ANSWER = "+ANSWER);}
}
运行结果:
ANSWER = 6
(完)
这篇关于LeetCode刷题:695. Max Area of Island(JAVA代码详解)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!