forked from moranzcw/LeetCode-NOTES
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.cpp
More file actions
28 lines (26 loc) · 790 Bytes
/
solution.cpp
File metadata and controls
28 lines (26 loc) · 790 Bytes
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
class Solution
{
public:
int islandPerimeter(vector<vector<int>>& grid)
{
int result = 0;
for(int i=0;i<grid.size();i++)
{
for(int j=0;j<grid[0].size();j++)
{
if(grid[i][j] == 1)
{
if( j-1 < 0 || grid[i][j-1] == 0)
result += 1;
if( j+1 == grid[0].size() || grid[i][j+1] == 0)
result += 1;
if( i-1 < 0 || grid[i-1][j] == 0)
result += 1;
if( i+1 == grid.size() || grid[i+1][j] == 0)
result += 1;
}
}
}
return result;
}
};