Skip to content

Commit 69a5c58

Browse files
committed
Add C Solution for 200-Number-Of-Islands
1 parent 1972d20 commit 69a5c58

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

c/200-Number-Of-Islands.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
3+
int directions[][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
4+
5+
void searchIsland(int row, int col, int rows, int cols, char** grid) {
6+
if ( row >= 0 && col >= 0 && row < rows && col < cols && grid[row][col] == '1') {
7+
for (int i = 0; i < 4; i++) {
8+
searchIsland(row + directions[i][0], col + directions[i][1], rows, cols, grid);
9+
grid[row][col] = '0';
10+
}
11+
}
12+
}
13+
14+
15+
int numIslands(char** grid, int gridSize, int* gridColSize){
16+
17+
int rows = gridSize;
18+
int cols = gridColSize[0];
19+
20+
int IslandCount = 0;
21+
22+
for (int row = 0; row < rows; row++) {
23+
for (int col = 0; col < cols; col++) {
24+
if (grid[row][col] == '1') {
25+
searchIsland(row, col, rows, cols, grid);
26+
IslandCount++;
27+
}
28+
}
29+
}
30+
return IslandCount;
31+
}
32+
33+
34+
35+
36+
// int numIslands(char** grid, int gridSize, int* gridColSize){
37+
// printf("%c", grid[0][4]);
38+
// return 0;
39+
// }

0 commit comments

Comments
 (0)