C++ Short and Fast, Beats 93%


  • 0
    M
    class Solution {
    public:
        int islandPerimeter(vector<vector<int>>& grid) {
            int p = 0;
            for (int i = 0; i < grid.size(); ++i)
                for (int j = 0; j < grid[i].size(); ++j)
                    if (grid[i][j] == 1)
                        p += (i == 0 || grid[i - 1][j] == 0) + (i == grid.size() - 1 || grid[i + 1][j] == 0) +
                             (j == 0 || grid[i][j - 1] == 0) + (j == grid[i].size() - 1 || grid[i][j + 1] == 0);
            return p;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.