Skip to content

Commit 49e36eb

Browse files
Create Island Perimeter.cpp
1 parent 66e7636 commit 49e36eb

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Easy Problems/Island Perimeter.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//https://leetcode.com/problems/island-perimeter/description/?envType=daily-question&envId=2024-04-18
2+
3+
class Solution {
4+
public:
5+
int islandPerimeter(vector<vector<int>>& grid) {
6+
7+
int n = grid.size();
8+
int m = grid[0].size();
9+
int cnt = 0;
10+
for (int i = 0; i < n; i++) {
11+
for (int j = 0; j < m; j++) {
12+
if (grid[i][j] == 1) {
13+
if ((j > 0 && grid[i][j - 1] == 0) || j == 0)
14+
cnt++;
15+
16+
if ((i > 0 && grid[i - 1][j] == 0) || i == 0)
17+
cnt++;
18+
19+
if ((j < m - 1 && grid[i][j + 1] == 0) || j == m - 1)
20+
cnt++;
21+
22+
if ((i < n - 1 && grid[i + 1][j] == 0) || i == n - 1)
23+
cnt++;
24+
}
25+
}
26+
}
27+
return cnt;
28+
}
29+
};

0 commit comments

Comments
 (0)