simple java solution


  • 0
    S

    count every square,if it next to another, count - 2

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


Log in to reply
 

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