LeetCode - 463. Island Perimeter - O(MN)- (C++) - 解题报告
原题
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代表陆地,0代表有水的地方。每个方格都是垂直或者水平连接的,没有斜线。被水包围的中间有一个小岛(小岛可能包含了很多个的陆地方格),小岛里没有岛中湖(内部的水和外部的水是连通的)。每个方格单元的边长都是1。各个地方都是成直角的,长度宽度不超过100,判断小岛的边长。
思路
判断其四面是否有陆地,四面都有陆地的对周长无贡献,两面有陆地的贡献周长为2,一面有陆地的贡献周长为3,四面都没有陆地的贡献周长为4。
代码
class Solution {
public:
int islandPerimeter(vector<vector<int>>& grid) {
int sum=0;
int m=grid.size(),n=grid[0].size();
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
if(grid[i][j]){
if(i==0||grid[i-1][j]==0) sum++;
if(i==m-1||grid[i+1][j]==0) sum++;
if(j==0||grid[i][j-1]==0) sum++;
if(j==n-1||grid[i][j+1]==0) sum++;
}
return sum;
}
};
LeetCode - 463. Island Perimeter - O(MN)- (C++) - 解题报告的更多相关文章
- LeetCode 463 Island Perimeter 解题报告
题目要求 You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 rep ...
- LeetCode 463. Island Perimeter (岛的周长)
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- [LeetCode] 463. Island Perimeter 岛的周长
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- LeetCode 463. Island Perimeter岛屿的周长 (C++)
题目: You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 repr ...
- LeetCode: 463 Island Perimeter(easy)
题目: You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 repr ...
- 3. leetcode 463 Island Perimeter
思路:设原始周长为4*节点数,每当出现一次相邻的情况,原始周长会减2.
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 463. Island Perimeter - LeetCode
Question 463. Island Perimeter Solution 题目大意:给出一个二维数组1表示陆地0表示海,求陆地的周长 思路: 重新构造一张地图grid2即一个二维数组,比原数组大 ...
- 【LeetCode】697. Degree of an Array 解题报告
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...
随机推荐
- oracle 导入数据报600错误
之前导入一个大容量dmp数据文件,报一个600错误,咨询网上的解决方法,按上面的处理一圈也没有整好,最后咨询组里一个大神,出现此错误 思路是,单个数据文件大小最大为32G,分析数据库后解决如下: 错误 ...
- Crash for small compressed texture on some Android device
I created a full white texture with 4x4 size. Unity requires that compressed texture size should be ...
- Xcode 9.3 pod install update 错误
[!] Oh no, an error occurred. Search for existing GitHub issues similar to yours: https://github.com ...
- Hibernate一级缓存和三种状态
Hibernate一级缓存又称session缓存,生命周期很短,跟session生命周期相同. 三种状态:1.transient(瞬时态):刚new出来的对象,既不在数据库中,也不在session管理 ...
- 哈希查找解决地址冲突的两种最常见方法(线性探测再散列,链地址法)C++实现
#include<iostream>#include<iomanip>using namespace std; typedef struct Node{ int data; s ...
- React的安装方法
一:直接使用 BootCDN 的 React CDN 库,地址如下: <script src="https://cdn.bootcss.com/react/16.4.0/umd/rea ...
- php 算法(二分法)只适用于有序表,且限于顺序存储结构
function demo($array,$low,$high,$k){ if($low<=$high){//判断该数组是否存在 $mid = intval(($low+$high)/2 ); ...
- Case Helper
using System; using Microsoft.Xrm.Sdk; using Microsoft.Crm.Sdk.Messages; using Microsoft.Xrm.Sdk.Que ...
- MAVLink功能开发,移植教程。
MAVLink功能开发 -----------------本文由"智御电子"提供,同时提供视频移植教程,以便电子爱好者交流学习.---------------- 1.MAVLink ...
- Waltz of love
Waltz of love Love me tenderly Love me softly Close your eyes,fling to the dangcing hall Follow your ...