릿코드(LEETCODE)
463. Island Perimeter
cepiloth
2020. 3. 3. 11:30
반응형

https://leetcode.com/problems/island-perimeter/
현재위치에서 4방향으로 이웃한 점이 있다면 그 개수만큼 -
class Solution {
public:
int islandPerimeter(vector<vector<int>>& grid) {
int count = 0;
int rows = grid.size();
int cols = grid[0].size();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (grid[i][j] == 1)
{
count += 4;
if (i - 1 >= 0 && grid[i - 1][j]) count--;
if (i + 1 < rows && grid[i + 1][j]) count--;
if (j - 1 >= 0 && grid[i][j - 1]) count--;
if (j + 1 < cols && grid[i][j + 1]) count--;
}
}
}
return count;
}
};
반응형