-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathIslandPerimeter.cpp
More file actions
32 lines (28 loc) · 812 Bytes
/
Copy pathIslandPerimeter.cpp
File metadata and controls
32 lines (28 loc) · 812 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
class Solution {
public:
int islandPerimeter(vector<vector<int>>& grid) {
int ans = 0, rows = grid.size();
if(!rows){
return ans;
}
int cols = grid[0].size();
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
if(grid[i][j] == 1){
ans = ans + 4;
if(j-1 >= 0){
if(grid[i][j-1] == 1){
ans = ans - 2;
}
}
if(i-1 >= 0){
if(grid[i-1][j] == 1){
ans = ans - 2;
}
}
}
}
}
return ans;
}
};