leetcode 463

题目描述:

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:

解题思路分析:

就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的边的数目即可。在查找重叠的边的数目的时候有一点小技巧,就是沿着其中两个方向就好,这种题目都有类似的规律,就是可以沿着上三角或者下三角形的方向来做。

一点感受:

题目非常简单,可是我为什么我还要把这个题目放到博客上呢?原因在于这是我第一次尝试直接在白板leetcode的白板上写代码,而且一次通过,算是开了个好头吧,以后不管是简单题还是难题,我都会写博客,以后面试的时候一定会用到。

如果编程语言用的比较熟,白板写代码的效果和IDE上写没太大区别。

class Solution {
public:
    int islandPerimeter(vector<vector<int>>& grid) {
        int res = 0, overlap = 0;
        for (int i = 0; i < grid.size(); i ++)
        {
            for (int j = 0; j < grid[i].size(); j ++)
            {
                if (grid[i][j] == 1)
                {
                    res ++;
                    if (j + 1 < grid[i].size() && grid[i][j + 1] == 1)
                        overlap ++;
                    if (i + 1 < grid.size() && grid[i + 1][j] == 1)
                        overlap ++;
                }
            }
        }

        return res * 4 - 2 * overlap;

    }
};

ps:代码风格可能还有点问题,接下来会好好改进的,毕竟程序员(工程师)是一种严谨的职业!

时间: 2024-08-07 15:08:02

leetcode 463的相关文章

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

leetcode 463. 岛屿的周长(Island Perimeter)

目录 题目描述: 示例 : 解法: 题目描述: 给定一个包含 0 和 1 的二维网格地图,其中 1 表示陆地 0 表示水域. 网格中的格子水平和垂直方向相连(对角线方向不相连).整个网格被水完全包围,但其中恰好有一个岛屿(或者说,一个或多个表示陆地的格子相连组成的岛屿). 岛屿中没有"湖"("湖" 指水域在岛屿内部且不和岛屿周围的水相连).格子是边长为 1 的正方形.网格为长方形,且宽度和高度均不超过 100 .计算这个岛屿的周长. 示例 : 输入: [[0,1,0

LeetCode 463. 岛屿的周长

题目链接:https://leetcode-cn.com/problems/island-perimeter/ 给定一个包含 0 和 1 的二维网格地图,其中 1 表示陆地 0 表示水域. 网格中的格子水平和垂直方向相连(对角线方向不相连).整个网格被水完全包围,但其中恰好有一个岛屿(或者说,一个或多个表示陆地的格子相连组成的岛屿). 岛屿中没有“湖”(“湖” 指水域在岛屿内部且不和岛屿周围的水相连).格子是边长为 1 的正方形.网格为长方形,且宽度和高度均不超过 100 .计算这个岛屿的周长.

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] 349 Intersection of Two Arrays &amp; 350 Intersection of Two Arrays II

这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/intersection-of-two-arrays/description/ 350 Intersection of Two Arrays II:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 题目&解法

LeetCode 442. Find All Duplicates in an Array (在数组中找到所有的重复项)

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twice in this array. Could you do it without extra space and in O(n) runtime? Example: Input: [4,3,2,7,