• 算法
    • 1.统计陆地(1)的个数
    • 2.统计重边个数,没遇到一个陆地检查其下方和右方是否也是陆地,如果是就含有一个重边
    • 3.4*陆地个数-2*重边个数即是岛屿周长
public int islandPerimeter(int[][] grid) {
    int row = grid.length;
    int col = grid[0].length;
    int grids = 0;
    int repeatEdges = 0;
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            if (grid[i][j] == 1) {
                grids++;
                if (i + 1 < row && grid[i+1][j] == 1) {
                    repeatEdges++;
                }
                if (j + 1 < col && grid[i][j+1] == 1) {
                    repeatEdges++;
                }
            }
        }
    }
    return 4 * grids - 2 * repeatEdges;
}
func islandPerimeter(grid [][]int) int {
    var row = len(grid)
    var col = len(grid[0])
    var grids = 0
    var repeatEdges = 0
    for i := 0; i < row; i++ {
        for j := 0; j < col; j++ {
            if grid[i][j] == 1 {
                grids++
                if i + 1 < row && grid[i+1][j] == 1 {
                    repeatEdges++
                }
                if j + 1 < col && grid[i][j+1] == 1 {
                    repeatEdges++
                }
            }
        }
    }
    return 4 * grids - 2 * repeatEdges
}