LeetCode 463. 岛屿的周长

题目链接:https://leetcode-cn.com/problems/island-perimeter/

给定一个包含 0 和 1 的二维网格地图,其中 1 表示陆地 0 表示水域。

网格中的格子水平和垂直方向相连(对角线方向不相连)。整个网格被水完全包围,但其中恰好有一个岛屿(或者说,一个或多个表示陆地的格子相连组成的岛屿)。

岛屿中没有“湖”(“湖” 指水域在岛屿内部且不和岛屿周围的水相连)。格子是边长为 1 的正方形。网格为长方形,且宽度和高度均不超过 100 。计算这个岛屿的周长。

示例 :

输入:
[[0,1,0,0],
[1,1,1,0],
[0,1,0,0],
[1,1,0,0]]

输出: 16

解释: 它的周长是下面图片中的 16 个黄色的边:

 1 int islandPerimeter(int** grid, int gridSize, int* gridColSize){
 2     if(grid==NULL||gridSize==0) return 0;
 3     int i,j,n=gridSize,m=gridColSize[0];
 4     int sum=0;
 5     for(i=0;i<n;i++){
 6         for(j=0;j<m;j++){
 7             if(grid[i][j]==1){
 8                 sum+=4;
 9                 if(i>0&&grid[i-1][j]==1) sum-=2;
10                 if(j>0&&grid[i][j-1]) sum-=2;
11             }
12         }
13     }
14     return sum;
15 }

原文地址:https://www.cnblogs.com/shixinzei/p/12501799.html

时间: 2024-10-28 14:42:45

LeetCode 463. 岛屿的周长的相关文章

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

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

[Swift]LeetCode463. 岛屿的周长 | 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 200.岛屿的数量 - DFS、BFS

Leetcode 200 岛屿的数量: DFS利用函数调用栈保证了检索顺序, BFS则需要自己建立队列,把待检索对象按规则入队. class Solution { // DFS解法,8ms/10.7MB,99.7% / 92% public: /** * row,col: 坐标,以0开始. * 用row和col,而不是x,y. 否则容易写成grid[x][y],顺序不对!! */ void dfs_set_zero(vector<vector<char>>& grid, i

[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 200. 岛屿数量(dfs||bfs)

给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 1: 输入:11110110101100000000 输出: 1示例 2: 输入:11000110000010000011 输出: 3 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/number-of-islands class Solution { pu

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

题目描述: 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 on

LeetCode | 200. 岛屿数量

原题(Medium): 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 思路:递归 这题类型跟我们之前做过的一题单词搜索类似.所以我在看到这题之后,就想到了可以用与之相同的思路:建立一个代表搜索方向的数组,在当前坐标下,如果往某个方向移动一格是符合一定条件的,就往该方向移动一格,且是递归形式的移动.在这里条件就是不超越边界,且该方向上有岛屿可供移动,且是未被访问过