Clear C Solution


  • 0
    int islandPerimeter(int** grid, int gridRowSize, int gridColSize) {
        int perimeter = 0;
        for(int i=0;i<gridRowSize;++i){
            for(int j=0; j<gridColSize;++j){
                if(grid[i][j])
                    perimeter += outsideEdgesNum(grid, i, j, gridRowSize, gridColSize);
            }
        }
        
        return perimeter;
    }
    
    int outsideEdgesNum(int **grid, int row, int col, int gridRowSize, int gridColSize){
        int outsideEdges = 0;
        if(row-1<0 || !grid[row-1][col]){
            outsideEdges += 1;
        }
        
        if(row+1==gridRowSize || !grid[row+1][col]){
            outsideEdges += 1;
        }
        
        if(col-1<0 || !grid[row][col-1]){
            outsideEdges += 1;
        }
        
        if(col+1==gridColSize || !grid[row][col+1]){
            outsideEdges += 1;
        }
        
        return outsideEdges;
    }
    

Log in to reply
 

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