/
NumIslandsClass.cs
42 lines (40 loc) · 1.11 KB
/
NumIslandsClass.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Async
{
public class NumIslandsClass
{
public int NumIslands(char[,] grid)
{
if (grid.Length == 0) return 0;
var result = 0;
for (int i = 0; i < grid.GetLength(0); i++)
{
for (int j = 0; j < grid.GetLength(1); j++)
{
if (grid[i, j] == '1')
{
result++;
ToTwo(grid, i, j);
}
}
}
return result;
}
private void ToTwo(char[,] grid, int i, int j)
{
if (i < 0 || j < 0 || i >= grid.GetLength(0) || j >= grid.GetLength(1) || grid[i, j] != '1')
{
return;
}
grid[i, j] = '2';
ToTwo(grid, i, j + 1);
ToTwo(grid, i, j - 1);
ToTwo(grid, i - 1, j);
ToTwo(grid, i + 1, j);
}
}
}