Description
Submission
class Solution {
public:
int islandPerimeter(vector<vector<int>>& grid) {
res = 0;
height = grid.size();
width = grid[0].size();
for(int i = 0; i < height; ++i) {
for(int j = 0; j < width; ++j) {
if(grid[i][j] == 1)
res += boxPerimeter(i, j, grid);
}
}
return res;
}
private:
int res;
int width;
int height;
int boxPerimeter(int i, int j, vector<vector<int>>& grid)
{
return getBoxAttr(i - 1, j, grid) + getBoxAttr(i + 1, j, grid) +
getBoxAttr(i, j - 1, grid) + getBoxAttr(i, j + 1, grid);
}
int getBoxAttr(int i, int j, vector<vector<int>>& grid)
{
if(i < 0 || i == height || j < 0 || j == width) return 1;
if(grid[i][j] == 1) return 0;
return 1;
}
};