-
463. Island Perimeter릿코드(LEETCODE) 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; } };
반응형'릿코드(LEETCODE)' 카테고리의 다른 글
1366. Rank Teams by Votes (0) 2020.03.03 733. Flood Fill (0) 2020.03.03 1207. Unique Number of Occurrences (0) 2020.02.28 1310. XOR Queries of a Subarray (0) 2020.02.16 1309. Decrypt String from Alphabet to Integer Mapping (0) 2020.02.16