C++ solution simple


  • 0
    J
    class Solution {
    public:
        int count_neighbours(vector<vector<int>>& grid,int l, int b, int i, int j)
        {
            int count = 0;
    
            if(j-1==-1 || !grid[i][j-1])
                count++;
            if(j+1==b || !grid[i][j+1])
                count++;
            if(i-1==-1 || !grid[i-1][j])
                count++;
            if(i+1==l || !grid[i+1][j])
                count++;
            return count;
        }
        int islandPerimeter(vector<vector<int>>& grid) {
            int l = grid.size();
            if(l==0)
                return 0;
            int b = grid[0].size();
            if(b==0)
                return 0;
            int ans = 0;
            for(int i=0;i<grid.size();i++)
                for(int j=0;j<grid[i].size();j++)
                    if(grid[i][j])
                        ans += count_neighbours(grid,l,b,i,j);
            return ans;       
            
        }
    };
    

Log in to reply
 

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