-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathISLAND_PERIMETER.cpp
More file actions
34 lines (32 loc) · 877 Bytes
/
ISLAND_PERIMETER.cpp
File metadata and controls
34 lines (32 loc) · 877 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
29
30
31
32
33
34
class Solution {
public:
int islandPerimeter(vector<vector<int>>& grid) {
int res = 0;
int r = grid.size();
int c = grid[0].size();
int i,j;
for(i=0;i<r;i++){
for(j=0;j<c;j++){
if(grid[i][j] == 0)
continue;
if(i==0)
res++;
if(i==r-1)
res++;
if(j==0)
res++;
if(j==c-1)
res++;
if(i>0 && grid[i-1][j]==0)
res++;
if(i<r-1 && grid[i+1][j]==0)
res++;
if(j>0 && grid[i][j-1]==0)
res++;
if(j<c-1 && grid[i][j+1]==0)
res++;
}
}
return res;
}
};