本文主要是介绍695. Max Area of Island(Leetcode每日一题-2020.03.15),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Problem
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.)
Note: The length of each dimension in the given grid does not exceed 50.
Example1
[[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.
EXample2
[[0,0,0,0,0,0,0,0]]
Given the above grid, return 0.
Solution
DFS
辅助二维数组visited,标识每一个位置是否访问过。
class Solution {
public:int maxAreaOfIsland(vector<vector<int>>& grid) {int rows = grid.size();if(rows == 0)return 0;int maxIslandArea = 0;int cols = grid[0
这篇关于695. Max Area of Island(Leetcode每日一题-2020.03.15)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!