clean c++ code one pass


  • 0
      int islandPerimeter(vector<vector<int>>& grid)
      { 
        int n=grid.size();
        int m=grid[0].size();
        int cnt=0;
        int cnt1=0;
        for(int i=0;i<n;++i)
            for(int j=0;j<m;++j)
            {   
                    if(grid[i][j]==1)
                    {
                        ++cnt1;
                        if(i!=n-1&&grid[i+1][j]==1)
                            ++cnt;
                        if(j!=m-1&&grid[i][j+1]==1) 
                            ++cnt;
                    }
            }
        return cnt1*4-cnt*2;
    }

Log in to reply
 

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