Solution in C beat 100% !???


  • 0
    I

    int islandPerimeter(int** grid, int gridRowSize, int gridColSize) {
    int i = 0, j = 0, sum = 0;

    for(i = 0; i < gridRowSize; i++){
        for(j = 0; j < gridColSize; j++){
            if(grid[i][j] == 1){
                // check up
                if(i == 0){
                    sum+=1;
                }else if((i > 0)&&(grid[i-1][j] == 0)){
                    sum+=1;
                }
                // check down
                if(i == gridRowSize-1){
                    sum+=1;
                }else if((i < gridRowSize-1)&&(grid[i+1][j] == 0)){
                    sum+=1;
                }
                // check left
                if(j == 0){
                    sum+=1;
                }else if((j > 0)&&(grid[i][j-1] == 0)){
                    sum+=1;
                }
                // check right
                if(j == gridColSize-1){
                    sum+=1;
                }else if((j < gridColSize-1)&&(grid[i][j+1] == 0)){
                    sum+=1;
                }
            }
        }
    }
    return sum;
    

    }


Log in to reply
 

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