straight foward C++ solution


  • 0
    K
    class Solution {
    public:
        int islandPerimeter(vector<vector<int>>& grid) {
            if(grid.empty()) return 0;
            int m=grid.size(),n=grid[0].size();
            int res=0;
            for(int i=0;i<m;++i)
                for(int j=0;j<n;++j)
                    if(grid[i][j])
                    {
                        if(!i||!grid[i-1][j]) res++;
                        if(i==m-1||!grid[i+1][j]) res++;
                        if(!j||!grid[i][j-1]) res++;
                        if(j==n-1||!grid[i][j+1]) res++;
                    }
            return res;
        }
    };

Log in to reply
 

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