my C solution (Overall Perimeter-repeat)


  • 0
    C

    int islandPerimeter(int** grid, int gridRowSize, int gridColSize) {
    int count=0;
    int repeat=0;
    for(int i=0;i!=gridRowSize;++i)
    {
    for(int j=0;j!=gridColSize;++j)
    {
    if(grid[i][j]==1)
    {
    count+=1;
    if(j<gridColSize-1)
    if(grid[i][j]==grid[i][j+1])
    repeat+=1;
    }
    }
    }

    for(int i=0;i!=gridColSize;++i)
    {
        for(int j=0;j!=gridRowSize;++j)
        {
            if(j<gridRowSize-1)
            if(grid[j][i]==1 && grid[j+1][i]==grid[j][i])
                repeat+=1;
        }
    }
    
    return 4*count-2*repeat;
    

    }


Log in to reply
 

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