Easy to read and short in C++


  • 0
    S
    int islandPerimeter(vector<vector<int>>& grid) {
        int m=grid.size();
        int n=grid[0].size();
        int count=0;
        for(int i=0;i<m;i++){           
            for(int j=0;j<n;j++){                
                count+=((i==m-1&&grid[i][j])+(i==0&&grid[i][j])+(i!=m-1&&grid[i][j]^grid[i+1][j]));                
                count+=((j==n-1&&grid[i][n-1])+(j==0&&grid[i][0])+(j!=n-1&&grid[i][j]^grid[i][j+1]));                 
            }
        }
        return count;
    }

Log in to reply
 

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