【跟Leon一起刷LeetCode】463. Island Perimeter

Island Perimeter

Description:

You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells). The island doesn’t have “lakes” (water inside that isn’t connected to the water around the island). One cell is a square with side length 1. The grid is rectangular, width and height don’t exceed 100. Determine the perimeter of the island.

Example 1:
[[0,1,0,0],
 [1,1,1,0],
 [0,1,0,0],
 [1,1,0,0]]

Answer: 16
Explanation: The perimeter is the 16 yellow stripes in the image below:
island

问题描述:

给定一个二维数组表示一个网格,1代表岛,0代表水。网格是完全被水环绕的,只有一个岛(一个或多个接壤的陆地cell),对角是不相连接的。一个cell的边长为1。求小岛的周长。

Solution:

解法一:
找到每个陆地,并且计算出每个陆地接壤的水cell的个数(假设边界外面包了一圈水cell)。

class Solution {
    public int islandPerimeter(int[][] grid) {
        int row = grid.length;
        int col = grid[0].length;
        int perimeter = 0;
        for(int i = 0; i < row; i++){
           for(int j = 0; j < col; j++){
                if (grid[i][j] == 0){
                    continue;
                }else{
                    if (i - 1 < 0 ) perimeter++;
                    else if (grid[i-1][j] == 0) perimeter++;
                    if (i + 2 > row) perimeter++;
                    else if (grid[i+1][j] == 0) perimeter++;

                    if (j - 1 < 0 ) perimeter++;
                    else if (grid[i][j-1] == 0) perimeter++;
                    if (j + 2 > col) perimeter++;
                    else if (grid[i][j+1] == 0) perimeter++; 

                }
            } 
        }
        return perimeter;
    }
}

解法二:
把它看成一个数学问题:

+--+     +--+                   +--+--+
|  |  +  |  |          ->       |     |
+--+     +--+                   +--+--+

islands * 4 - neighbours * 2,每次合并都会在总周长的基础上减2,形成环形的[[1,1],[1,1]]时候要减4.总结下来就是上面的公式。这个时候只要遍历所有的陆地cell,找到它们的邻居。遍历时,是找两个方向,左上或者右下邻居,因为两个方向即可遍历完这个网格,且不会重复。

public class Solution {
    public int islandPerimeter(int[][] grid) {
        int islands = 0, neighbours = 0;

        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                if (grid[i][j] == 1) {
                    islands++; // count islands
                    if (i < grid.length - 1 && grid[i + 1][j] == 1) neighbours++; // count down neighbours
                    if (j < grid[i].length - 1 && grid[i][j + 1] == 1) neighbours++; // count right neighbours
                }
            }
        }

        return islands * 4 - neighbours * 2;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值