-
Notifications
You must be signed in to change notification settings - Fork 9
/
IslandPerimeter.cpp
32 lines (29 loc) · 1.03 KB
/
IslandPerimeter.cpp
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
// Problem: https://leetcode.com/problems/island-perimeter/
#include <vector>
using namespace std;
class IslandPerimeter {
public:
int islandPerimeter(vector<vector<int>>& grid) {
int max_rows = grid.size();
int max_cols = grid[0].size();
int count = 0;
for (int r = 0; r < max_rows; ++r) {
for (int c = 0; c < max_cols; ++c) {
int curr = grid[r][c];
if (curr == 0) continue;
count += getVal(grid, r, c + 1, max_rows, max_cols) +
getVal(grid, r + 1, c, max_rows, max_cols) +
getVal(grid, r - 1, c, max_rows, max_cols) +
getVal(grid, r, c - 1, max_rows, max_cols);
}
}
return count;
}
private:
int getVal(vector<vector<int>>& grid, int row, int col, int max_rows, int max_cols) {
if (row < 0 || col < 0 ||
row >= max_rows || col >= max_cols ||
(grid[row][col] == 0)) return 1;
return 0;
}
};