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: ...
随机推荐
- ASP.NET中GridView数据导出到Excel
/// <summary> /// 导出按钮 /// </summary> /// <param name="sender"></para ...
- EXT5 时间框控制(开始时间不能大于结束时间)
1.网上看的大部分代码都是利用vtype : 'dateRange' EXT的这个属性,但是可能由于环境问题还是怎么样,我就是实现不了想要的效果. 然后研究了一下,在时间框的listeners 监听 ...
- 并查集+树链剖分+线段树 HDOJ 5458 Stability(稳定性)
题目链接 题意: 有n个点m条边的无向图,有环还有重边,a到b的稳定性的定义是有多少条边,单独删去会使a和b不连通.有两种操作: 1. 删去a到b的一条边 2. 询问a到b的稳定性 思路: 首先删边考 ...
- Codeforces 486E LIS of Sequence 题解
题目大意: 一个序列,问其中每一个元素是否为所有最长上升子序列中的元素或是几个但不是所有最长上升子序列中的元素或一个最长上升子序列都不是. 思路: 求以每一个元素为开头和结尾的最长上升子序列长度,若两 ...
- 关于2016.12.12——T1的反思:凸包的意义与应用
2016.12.12 T1 给n个圆,保证圆圆相离,求将圆围起来的最小周长.n<=100 就像上图.考场上,我就想用切线的角度来做凸包.以圆心x,y排序,像点凸包一样,不过用两圆之间的下切线角度 ...
- 【JBOSS】数据库连接配置小结
数据库驱动位置: %JBOSS_HOME%\server\default\lib目录下. 数据库配置文件位置:JBOSS_HOME\docs\examples\jca\XXXX-ds.xml < ...
- db2 日期时间格式
db2日期和时间常用汇总 1.db2可以通过SYSIBM.SYSDUMMY1.SYSIBM.DUAL获取寄存器中的值,也可以通过VALUES关键字获取寄存器中的值. SELECT 'HELLO DB2 ...
- 关于zepto(相似于jquery、jQuery用于网页浏览器,zepto用于手机浏览器)
http://blog.csdn.net/kongjiea/article/details/42522305 -----关于zepto和jquery的差别 jQuery 使用 .width() 和 ...
- Total Commander解压位置
TC解压到当前文件夹下 TC也是用了一段时间,现在勉强也算用习惯了,今天在解压文件的时候感觉步骤麻烦,之前解压都是解压到另一个窗口,所以一直是ALT+8同步窗口,然后解压文件.但一般解压文件都是解压到 ...
- java项目启动后,数据库字段生成 user_name带下划线这种形式的
hibernate 5.0 版本以上去掉了 hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy 这个属性,如果非 ...