generated from eyamenko/dotnet-template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem32.cs
More file actions
48 lines (43 loc) · 1.44 KB
/
Problem32.cs
File metadata and controls
48 lines (43 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
namespace LeetCode;
/// <summary>
/// <see href="https://leetcode.com/problems/number-of-islands/">Number of Islands</see>.
/// </summary>
public static class Problem32
{
/// <summary>
/// Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.
/// An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically.
/// You may assume all four edges of the grid are all surrounded by water.
/// Time complexity: O(m * n).
/// Space complexity: O(m * n).
/// </summary>
/// <param name="grid">Array to traverse.</param>
/// <returns>Number of islands.</returns>
public static int NumIslands(char[][] grid)
{
var count = 0;
for (var m = 0; m < grid.Length; m++)
{
for (var n = 0; n < grid[m].Length; n++)
{
if (grid[m][n] == '1')
{
count++;
Traverse(grid, m, n);
}
}
}
return count;
}
private static void Traverse(char[][] grid, int m, int n)
{
if (m >= 0 && m < grid.Length && n >= 0 && n < grid[m].Length && grid[m][n] == '1')
{
grid[m][n] = '*';
Traverse(grid, m + 1, n);
Traverse(grid, m - 1, n);
Traverse(grid, m, n + 1);
Traverse(grid, m, n - 1);
}
}
}