Skip to content

Commit

Permalink
Time: 23 ms (92.62%), Space: 12.5 MB (67.49%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipendra-Raghav committed Sep 24, 2023
1 parent e87f014 commit 8a8f69a
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions 0200-number-of-islands/0200-number-of-islands.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Solution {
public:
int n;
int m;
void solve(int i,int j,vector<vector<char>> &grid)
{
if(i<0 || j>=m || i>=n || j<0 || grid[i][j]=='0' || grid[i][j]=='-')return;

grid[i][j]='-';
solve(i-1,j,grid);
solve(i,j-1,grid);
solve(i+1,j,grid);
solve(i,j+1,grid);

}
int numIslands(vector<vector<char>>& grid) {
n=grid.size();
m=grid[0].size();
if(grid.size()==0)
return 0;
int ans=0;

for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(grid[i][j]=='1')
{
solve(i,j,grid);
ans++;
}
}
}
return ans;

}
};

0 comments on commit 8a8f69a

Please sign in to comment.