leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0463.cpp (700B)


      1 class Solution {
      2   public:
      3     int islandPerimeter(const vector<vector<int>> &grid) const {
      4         const int n = size(grid), m = size(grid[0]);
      5         static int offset[] = {-1, 0, 1, 0, -1};
      6         int res = 0;
      7 
      8         for (int i = 0; i < n; i++) {
      9             for (int j = 0; j < m; j++) {
     10                 if (!grid[i][j]) continue;
     11 
     12                 res += 4;
     13                 for (int k = 0; k < 4; k++) {
     14                     const int x = i + offset[k];
     15                     const int y = j + offset[k + 1];
     16                     if (x < 0 || y < 0 || x >= n || y >= m) continue;
     17                     res -= grid[x][y] == 1;
     18                 }
     19             }
     20         }
     21 
     22         return res;
     23     }
     24 };