-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0463-IslandPerimeter.cs
33 lines (30 loc) · 981 Bytes
/
0463-IslandPerimeter.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
//-----------------------------------------------------------------------------
// Runtime: 176ms
// Memory Usage: 29.2 MB
// Link: https://leetcode.com/submissions/detail/334992582/
//-----------------------------------------------------------------------------
namespace LeetCode
{
public class _0463_IslandPerimeter
{
public int IslandPerimeter(int[][] grid)
{
var result = 0;
var row = grid.Length;
var col = grid[0].Length;
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
{
if (grid[i][j] == 1)
{
result += 4;
if (i > 0 && grid[i - 1][j] == 1)
result -= 2;
if (j > 0 && grid[i][j - 1] == 1)
result -= 2;
}
}
return result;
}
}
}