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

class Solution {
public:
int islandPerimeter(vector<vector<int>>& grid) {
int ret=0;
int row=grid.size(),col=grid[0].size();
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
{
if(grid[i][j]==0)
continue;
if(i==0||grid[i-1][j]==0)ret++;
if(i==row-1||grid[i+1][j]==0) ret++;
if(j==0||grid[i][j-1]==0) ret++;
if(j==col-1||grid[i][j+1]==0) ret++;
}
return ret;
}
};
Leetcode-463 Island Perimeter的更多相关文章
- 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 解题报告
题目要求 You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 rep ...
- 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 - O(MN)- (C++) - 解题报告
原题 原题链接 You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 ...
- 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.
- 463. Island Perimeter - LeetCode
Question 463. Island Perimeter Solution 题目大意:给出一个二维数组1表示陆地0表示海,求陆地的周长 思路: 重新构造一张地图grid2即一个二维数组,比原数组大 ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】463. Island Perimeter 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 减去相交部分 参考资料 日期 题目地址:https: ...
随机推荐
- 动作手游实时PVP帧同步方案(客户端)
1.概述 1.1.基于UDP的帧同步方案 在技术选型方面,之所以选择帧同步方案,在Kevin的一篇介绍PVP帧同步后台实现的文章中已经做了详细叙述,这里简单摘要如下: 高一致性.如果每一帧的输入都同步 ...
- C# Current thread must be set to single thread apartment (STA) mode before OLE calls can be made
将箭头指向部分替换为编译器报错的内容即可. 参考文章:https://www.experts-exchange.com/questions/28238490/C-help-needed-Current ...
- C# DataGridView绑定数据源
第一种: DataSet ds=new DataSet (); ]; 第二种: DataTable dt=new DataTable(); this.dataGridView1.DataSource= ...
- files list file for package 'xxx' is missing final newline
#!/usr/bin/python # 8th November, 2009 # update manager failed, giving me the error: # 'files list f ...
- CODEVS 天梯 代码记录
所有水题均被折叠 Lv.1 青铜 1201 #include<iostream> #include<cstring> #include<algorithm> #in ...
- bzoj1051Tarjan裸题
tarjan缩点+判断出度为0的点 所以不需要新建边 #include <cstdio> ,time=,T=,sum=,ans=; ],to[],nex[],fir[],dfn[],low ...
- 华清远见成为ARM大学计划正式合作伙伴
来源:华清远见嵌入式学院 近日,华清远见教育集团成为ARM大学计划合作伙伴,这是ARM大学计划合作伙伴中的国内唯一教育机构.此次合作是ARM公司对华清远见教育集团的高度认可,也充分证明了华清远见这些年 ...
- <input>和<textarea>
作为一个刚刚涉足PHP开发的菜鸟,第一次使用博客.在这里分享一些经验给和需要的朋友,互相探讨.共同学习,希望对你有所帮助. 废话不多说,下面进入正题. 应该有朋友和我一样,需要用到文本框,要求它会自动 ...
- Cannot connect to the Docker daemon. Is the docker daemon running on this host?
解决方案 docker-machine restart&&eval "$(docker-machine env default)"&&docker- ...
- java分享第二十天(build.xml的语法及写法)
通常情况下,Ant构建文件build.xml应该在项目的基础目录.可以自由使用其他文件名或将构建文件中其他位置.在本练习中,创建一个名为build.xml 在电脑的任何地方的文件. <?xml ...