每日一题ing,今天是个medium题695. Max Area of Island
class Solution {
public:
int maxAreaOfIsland(vector<vector<int>>& grid) {
int ret=0;
for(int i=0;i<grid.size();i++){
for(int j=0;j<grid[i].size();j++){
if(grid[i][j]==1)
ret=max(ret,dfs(grid,i,j));
}
}
return ret;
}
int dfs(vector<vector<int>>& grid,int i,int j){
if(i<0||j<0||i>=grid.size()||j>=grid[i].size()||grid[i][j]==0){
return 0;
}
grid[i][j]=0;
int num=1;
num=num+dfs(grid,i+1,j);
num=num+dfs(grid,i,j+1);
num=num+dfs(grid,i-1,j);
num=num+dfs(grid,i,j-1);
return num;
}
};
|