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: ...
随机推荐
- <转>Unity3D研究院之C#使用Socket与HTTP连接服务器传输数据包
最近项目中需要使用HTTP与Socket,把自己这段时间学习的资料整理一下.有关Socket与HTTP的基础知识MOMO就不赘述拉,不懂得朋友自己谷歌吧.我们项目的需求是在登录的时候使用HTTP请求, ...
- ubuntu14.04+nvidia driver+cuda8+cudnn5+tensorflow0.12
文章在简书里面编辑的,复制过来貌似不太好看,还是到简书的页面看吧: http://www.jianshu.com/p/c89b97d052b7 1.安装环境简介: 硬件: cpu:i7 6700k g ...
- warning C4996: 'sprintf': This function or variable may be unsafe
选项Project | Configuration Properties | C/C++ | Preprocessor | Preprocessor Defin ...
- dotnetcore 单元测试
dotnetcore的单元测试目前支持的比较好的是xunit,首先通过nuget添加组件dotnet-test-xunit 和 xunit.如果有依赖注入可在构造方法中,相当于Nunit中的[Setu ...
- Smack4.1注册新用户
更新了smack4.1后,发现之前的注册表单不能使用了,很多属性都不能使用. 发现4.1把帐号的出来都集中到 org.jivesoftware.smackx.iqregister.AccountMan ...
- python中类的三种属性
python中的类有三种属性:字段.方法.特性 字段又分为动态字段和静态字段 类 class Province: #静态字段 memo = 'listen' #动态字段 def __init__(se ...
- Angularjs ng-if和ng-show的区别
ng-if:判断条件,为true时向html中插入节点,否则从html中移除节点: ng-show: 简单的显示和隐藏(display) 坑点:ng-if会创建一个新的作用域(scope),如果内部元 ...
- 【转】【10g SQL新特性】q-quote使用
转自:http://blog.chinaunix.net/uid-7655508-id-3684042.html 转发只为留存学习 在Oracle中,字符串的字面量如果含有单引号,那么必须转义,而且转 ...
- Logstash之multiline 插件
input { stdin { codec => multiline { pattern => "^\[" negate => true what => & ...
- python奇偶数求和
#求100内奇数和while\for..in循环 sum = 0 i = 1 while i <= 100: sum += i i += 2 print(sum) sum = 0 for i i ...