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,面积加一,同时将之值置零,访问其四周
代码1:
class Solution {
public:
int findarea(vector<vector<int>>& grid, int i, int j){
int area = 0;
if((i-1)>=0 && grid[i-1][j]) {
grid[i-1][j] = 0;
area += (1 + findarea(grid,(i-1),j) );
};// up
if((i+1)<grid.size() && grid[i+1][j]) {
grid[i+1][j] = 0;
area += (1 + findarea(grid,(i+1),j) );
};// down
if((j-1)>=0 && grid[i][j-1]) {
grid[i][j-1] = 0;
area += (1 + findarea(grid,i,(j-1) ));
};// left
if((j+1)<grid[0].size() && grid[i][j+1]) {
grid[i][j+1] = 0;
area += (1 + findarea(grid,i,(j+1) ));
};// right
return area;
}
int maxAreaOfIsland(vector<vector<int>>& grid) {
int i=0,j, area = 0,line, column;
line = grid.size();
column = grid[0].size();
int maxarea = 0;
for(;i<line;i++){
j=0;
for(;j<column;j++){
if (grid[i][j]){
grid[i][j] = 0;
area =1 + findarea(grid,i,j) ;
if (area > maxarea){
maxarea = area;
}
}
}
}
return maxarea;
}
};
代码2:
class Solution {
public:
int helper(vector<vector<int>>& grid, int i, int j, int area){
grid[i][j] = 0;
if(i-1>=0 && grid[i-1][j]){//up
area++;
area = helper(grid,i-1,j,area);//不是 area += helper(grid,i-1,j,area);,你的加体现在area++了
}
if(j-1>=0 && grid[i][j-1]){//left
area++;
area = helper(grid,i,j-1,area);
}
if(i+1<grid.size() && grid[i+1][j]){//down
area++;
area = helper(grid,i+1,j,area);
}
if(j+1<grid[0].size() && grid[i][j+1]){//right
area++;
area = helper(grid,i,j+1,area);
}
return area;
}
int maxAreaOfIsland(vector<vector<int>>& grid) {
int res=0;
for(int i=0;i<grid.size();i++){
for(int j=0;j<grid[0].size();j++){
if(grid[i][j]){
int area=1;
//grid[i][j] = 0;
area = helper(grid,i,j,area);
if(area>res){res=area;}
}
}
}
return res;
}
};
代码3
class Solution {
public:
int maxAreaOfIsland(vector<vector<int>>& grid) {
int max_area = 0;
for(int i = 0; i < grid.size(); i++)
for(int j = 0; j < grid[0].size(); j++)
if(grid[i][j] == 1)max_area = max(max_area, AreaOfIsland(grid, i, j));
return max_area;
}
int AreaOfIsland(vector<vector<int>>& grid, int i, int j){
if( i >= 0 && i < grid.size() && j >= 0 && j < grid[0].size() && grid[i][j] == 1){
grid[i][j] = 0;
return 1 + AreaOfIsland(grid, i+1, j) + AreaOfIsland(grid, i-1, j) + AreaOfIsland(grid, i, j-1) + AreaOfIsland(grid, i, j+1);
}
return 0;
}
};
效率更高