Golang solution


  • 0
    R
    func islandPerimeter(grid [][]int) int {
        counter := 0
            for i := 0; i < len(grid); i++ {
                for j := 0; j < len(grid[0]); j++ {
                    if(grid[i][j] == 1) {
                        if (i - 1 < 0 || grid[i-1][j] == 0) {
                            counter += 1
                        }
                        if i + 1 >= len(grid) || grid[i+1][j] == 0 {
                            counter += 1
                        }
                        if j - 1 < 0 || grid[i][j-1] == 0 {
                            counter += 1
                        }
                        if j + 1 >= len(grid[0]) || grid[i][j+1] == 0 {
                            counter += 1
                        }
                    }
                }
            }
            return counter;
    }
    

Log in to reply
 

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