Island Perimeter Leetcode

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:

这道题我是按照遍历,看看如果周边有1的话周长就减1的做法做的。

public class Solution {
    public int islandPerimeter(int[][] grid) {
        if (grid == null || grid.length == 0 || grid[0].length == 0) {
            return 0;
        }
        int count = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == 1) {
                    count += 4 + isOne(grid, i, j);
                }
            }
        }
        return count;
    }
    private boolean inBound(int[][] grid, int x, int y) {
        if (x < 0 || x >= grid.length || y < 0 || y >= grid[0].length) {
            return false;
        }
        return true;
    }
    private int isOne(int[][] grid, int x, int y) {
        int count = 0;
        int[] dx = {0, 0, 1, -1};
        int[] dy = {1, -1, 0, 0};
        for (int i = 0; i < 4; i++) {
            int newX = x + dx[i];
            int newY = y + dy[i];
            if (inBound(grid, newX, newY) && grid[newX][newY] == 1) {
                count--;
            }
        }
        return count;
    }
}

做的可能有点复杂了,因为还有更好的做法。只要遍历右边和下边就可以了。最后结果是岛的个数 * 4 - 2 * 右边和下边的个数。因为右边和下边代表有两个岛要减一,所以乘2。

public class Solution {
    public int islandPerimeter(int[][] grid) {
        if (grid == null || grid.length == 0 || grid[0].length == 0) {
            return 0;
        }
        int count = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == 1) {
                    count += 4;
                    if (i + 1 < grid.length && grid[i + 1][j] == 1) {
                        count -= 2;
                    }
                    if (j + 1 < grid[0].length && grid[i][j + 1] == 1) {
                        count -= 2;
                    }
                }
            }
        }
        return count;
    }
}
时间: 2025-01-16 06:26:28

Island Perimeter Leetcode的相关文章

463. Island Perimeter - LeetCode

Question 463. Island Perimeter Solution 题目大意:给出一个二维数组1表示陆地0表示海,求陆地的周长 思路: 重新构造一张地图grid2即一个二维数组,比原数组大一圈,即长宽都大2 一个点在原地图坐标是(i,j),那么在重新构造的坐标就是(i+1,j+1) 遍历原地图,如果一是陆地,就遍历这个点的周围是否是海,如果是海线周长就加1 Java实现: public int islandPerimeter(int[][] grid) { int total = 0

Leetcode-463 Island Perimeter

#463. Island Perimeter 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 th

LeetCode Island Perimeter

原题链接在这里:https://leetcode.com/problems/island-perimeter/ 题目: 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 c

Leetcode 463. Island Perimeter JAVA语言

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 isla

leetcode算法:Island Perimeter

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 isla

LeetCode: 463 Island Perimeter(easy)

题目: 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

[LeetCode] Island Perimeter 岛屿周长

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 isla

[LeetCode] 463. Island Perimeter 岛的周长

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 isla

LeetCode 463 Island Perimeter 解题报告

题目要求 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