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

题目标签:Hash Table
题目给了我们一个2d grid,1 代表 岛;0 代表 海水,让我们找到岛的周长。
遍历grid,对于等于1 的cell: 首先res += 4,把它的周长先加上;然后检查它的 上方 和左边 的cell,如果是1的,就减去2。
因为我们的遍历方向是从上到下,从左到右,所以我们只需要检查 上方 和 左边的格子就可以了。
Java Solution:
Runtime beats 91.70%
完成日期:06/07/2017
关键词:Array
关键点:只需要检查上面和左边的邻居
class Solution
{
public int islandPerimeter(int[][] grid)
{
int res = 0; for(int i=0; i<grid.length; i++)
{
for(int j=0; j<grid[0].length; j++)
{
if(grid[i][j] == 1) // for each land, only check its up and left neighbor cell
{
res += 4; if(i > 0 && grid[i-1][j] == 1) // if up cell is 1, res - 2
res -= 2;
if(j > 0 && grid[i][j-1] == 1) // if left cell is 1, res - 2
res -= 2;
} }
} return res;
}
}
参考资料:
https://discuss.leetcode.com/topic/68786/clear-and-easy-java-solution
LeetCode 题目列表 - LeetCode Questions List
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岛屿的周长 (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 解题报告
题目要求 You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 rep ...
- 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 岛屿的周长
详见:https://leetcode.com/problems/island-perimeter/description/ C++: class Solution { public: int isl ...
- 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 ...
随机推荐
- 【转】jvm类加载
类加载机制 JVM把class文件加载的内存,并对数据进行校验.转换解析和初始化,最终形成JVM可以直接使用的Java类型的过程就是加载机制. 类从被加载到虚拟机内存中开始,到卸载出内存为止,它的生命 ...
- Linux 学习(四)
搭建jdk 安装jdk操作: 1.光驱挂载:mount /dev/cdrom /mnt 2.拷贝安装包至其他文件夹(如home目录下) 3.执行安装包(bin包:./包名) 4.配置环境变量:打开文件 ...
- Angular——内置服务
$location <!DOCTYPE html> <html lang="en" ng-app="App"> <head> ...
- centos上安装supervisor来管理dotnetcore等应用程序
supervisor 介绍: 这是一款用python编写的进程管理工具,可以守护他管理的所有进程,防止异常退出,以及提供一个可视化的web界面来手动管理,打开关闭重启各种应用,界面如下: 关于在cen ...
- python学习笔记- 补遗
1.extend 和 append区别 extend 和 append区别 #extend接受list参数,添加每个元素至原list尾端 >>> l=[1,2,3] >> ...
- 【笔记JS/HTML/CSS】用div实现个性化button,背景半透明
html中的button默认样式..不太能看,如果调一调背景色和字体的话也挺适合简洁的页面设计 于是决定配合JS,用html中的div完成button 最终结果图: html代码:(first_pas ...
- (转)Hibernate框架基础——映射普通属性
http://blog.csdn.net/yerenyuan_pku/article/details/52739871 持久化对象与OID 对持久化对象的要求 提供一个无参的构造器.使Hibernat ...
- HDU_1006_Tick and Tick
Tick and Tick Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- [iOS]查看苹果支持的所有字库
有时候,在开发的时候,想换个字体更加优雅的显示.但是不知道苹果本身到底支持了那些字体,怎么办? 没关系,打开这个网址:http://iosfonts.com/ 所见即所得,非常方便,而且还表明了从那个 ...
- 写给新手的十一条 Docker 守则
很多人最终还是决定使用 Docker 解决问题. Docker 的优点很多,比如: 一体化——将操作系统.库版本.配置文件.应用程序等全部打包装在容器里.从而保证 QA 所测试的镜像 (image) ...