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- ...
随机推荐
- 分布式网上商城项目-solr搜索功能错误
1.RuntimeException错误 java.lang.RuntimeException: org.apache.ibatis.binding.BindingException: Invalid ...
- 异常笔记:运行hdfs copyFromLocal 上传文件报错
把本地文件系统,复制到dfs文件系统时报错的错 [hadoop@localhost ~]$ hdfs dfs -copyFromLocal /home/hadoop/mk.txt /xg_test/ ...
- Java性能优化的50个细节
在JAVA程序中,性能问题的大部分原因并不在于JAVA语言,而是程序本身.养成良好的编码习惯非常重要,能够显著地提升程序性能. 1. 尽量在合适的场合使用单例 使用单例可以减轻加载的负担,缩短加载的时 ...
- 线上服务内存OOM问题定位三板斧
相信大家都有感触,线上服务内存OOM的问题,是最难定位的问题,不过归根结底,最常见的原因: 本身资源不够 申请的太多 资源耗尽 58到家架构部,运维部,58速运技术部联合进行了一次线上服务内存OOM问 ...
- git 完善使用中
GIT 版本库控制: 第一步:Git 的账号注册 url :https://github.com/ 这是git的官网如果第一次打开会这样 中间红色圈内是注册 内容, 第一项是用户名 第二项是邮箱 第三 ...
- windows下nginx的安装
一. 下载 http://nginx.org/ (下载后解压) 二. 修改配置文件 nginx配置文件在 nginx-1.8.0\conf\nginx.conf http { gzip on; ...
- 对象转换成JSON字符串
定义一个Student类: 1 class Student { 2 public $name; 3 public $age; 4 function __construct($name, $age) { ...
- day 28 黏包及黏包解决方案
1.缓冲区 每个socket被创建以后,都会分配两个缓冲区,输入缓冲区和输出缓冲区,默认大小都是8k,可以通过getsocket()获取,暂时存放传输数据,防止程序在发送的时候卡阻,提高代码运行效率. ...
- 集合之WeakHashMap
WeakHashMap 底层数据结构是哈希表结构 依赖于键的数据结构特点 不同于HashMap,该类的键是以弱键的形式存在 当该键成为垃圾对象,会被垃圾回收期空闲的时候回收,那么改键所对应值也会被回收 ...
- JS中数组方法的封装之slice
slice方法的功能 // 1) : 数组的截取 // 2) :slice(m,n): 从数组索引m开始,截取到索引n,但是不包含n;[前包后不包] // slice(m) : 从索引m开始,截取到末 ...