【LeetCode】463. Island Perimeter

问题描述

问题链接:https://leetcode.com/problems/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:

[[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:

我的代码

这个思路还蛮简单的,我直接贴代码了。

public class Solution {
    public int islandPerimeter(int[][] grid) {
        /*
        根据限制条件,每个1贡献的有效边长数=4-上下左右4个方格中1的数量
        */

        int m = grid.length;
        if(m == 0){
            return 0;
        }
        int n = grid[0].length;

        int perimeter = 0;

        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(grid[i][j] == 1){
                    perimeter += calcPerimeter(grid,i,j);
                }
            }
        }
        return perimeter;
    }

    private int calcPerimeter(int[][] grid, int x, int y){
        int m = grid.length;
        int n = grid[0].length;

        int count = 0;
        if(x > 0 && grid[x - 1][y] == 1){
            count++;
        }
        if(x < m - 1 && grid[x + 1][y] == 1){
            count++;
        }
        if(y > 0 && grid[x][y - 1] == 1){
            count++;
        }
        if(y < n - 1 && grid[x][y + 1] == 1){
            count++;
        }
        return 4 - count;
    }
}

打败了16.56%的Java代码。来看看讨论区的大神们。

讨论区

clear and easy java solution

链接地址:https://discuss.leetcode.com/topic/68786/clear-and-easy-java-solution

  • loop over the matrix and count the number of islands;
  • if the current dot is an island, count if it has any right neighbour or down neighbour;
  • the result is islands * 4 - neighbours * 2

代码如下:

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;
    }
}

关键在于每个邻居让2条边消失了,然后当前节点的左边和上边之前我们都数过了。

还有一个精简版的。

public static int islandPerimeter(int[][] grid) {
    if (grid == null || grid.length == 0 || grid[0].length == 0) return 0;
    int result = 0;
    for (int i = 0; i < grid.length; i++) {
        for (int j = 0; j < grid[0].length; j++) {
            if (grid[i][j] == 1) {
                result += 4;
                if (i > 0 && grid[i-1][j] == 1) result -= 2;
                if (j > 0 && grid[i][j-1] == 1) result -= 2;
            }
        }
    }
    return result;
}

代码来自https://discuss.leetcode.com/topic/68983/java-9-line-solution-add-4-for-each-land-and-remove-2-for-each-internal-edge/2

你可能感兴趣的:(LeetCode,leetcode)