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 (岛的周长)的更多相关文章

  1. [LeetCode] 463. Island Perimeter 岛的周长

    You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...

  2. 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 ...

  3. 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 ...

  4. LeetCode 463 Island Perimeter 解题报告

    题目要求 You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 rep ...

  5. 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 ...

  6. 3. leetcode 463 Island Perimeter

    思路:设原始周长为4*节点数,每当出现一次相邻的情况,原始周长会减2.

  7. 463 Island Perimeter 岛屿的周长

    详见:https://leetcode.com/problems/island-perimeter/description/ C++: class Solution { public: int isl ...

  8. 463. Island Perimeter - LeetCode

    Question 463. Island Perimeter Solution 题目大意:给出一个二维数组1表示陆地0表示海,求陆地的周长 思路: 重新构造一张地图grid2即一个二维数组,比原数组大 ...

  9. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

随机推荐

  1. linux 怎么用 名字 代替 ip ?

    比如 ssh 1.1.1.1 变成 ssh usr1 在每台机子的/etc/hosts文件中添加ip与名字的对应表

  2. dive into python:模块的导入和搜索文件路径的配置

    1.Python中导入模块:import sys:相当于Java中的导入包.类. 比如,我们导入sys模块,使用:import sys; 2.Python中调用函数的时候,会从默认配置的库文件夹中(s ...

  3. Angular——自定义服务

    基本介绍 之前我们介绍了angular内置的几种服务,这里我们介绍如何自己定义自己的服务,主要是通过三个方法:factory.service.value 基本使用 factory:可以返回对象,也可以 ...

  4. Windows10环境中 laravel任务调度 如何启动调度

    Windows10环境中 laravel任务调度 如何启动调度 一:问题由来 1:今天在做用laravel开发订单系统的时候,需要使用定时任务来大批量提交订单,测试一下订单金额是否有误.发现larav ...

  5. arx升级

    如果你打算升级你的ARX或者想在同一个IDE(譬如vs2010)编译多个版本的ARX,那么我希望这篇帖子对你有帮助首先你应该简单了解Objectarx开发的版本对应情况:R15   --- 2000- ...

  6. python的webservice请求

    1.pip install client 2.pip install suds-jurko #coding=utf-8from suds.client import Clientimport time ...

  7. Scrapy用Cookie实现模拟登录

    模拟登录是爬取某些站点内容的一个关键,有些网站(特别是论坛类),不登录的话,一个数据也拿不到. 模拟登录有这样几个关键: 弄清楚登录的url一些网站打开出现登录的页面,地址栏大多数不是登录提交表单的u ...

  8. Oracle 回滚(ROLLBACK)和撤销(UNDO)

    一.回滚(ROLLBACK)和撤销(UNDO) 回滚和前滚是保证Oracle数据库中的数据处于一致性状态的重要手段. 在9i版本以前 Oracle使用数据库中的回滚段来实现未提交数据或因系统故障导致实 ...

  9. 搭建一台功能简单的FTP服务器

    #vi /etc/sysconfig/network-scripts/ifcfg-eno33554952 #revice network restart #ping www.baidu.com #ip ...

  10. python字符串方法replace()简介

    今天写replace方法的时候的代码如下: message = "I really like dogs" message.replace('dog','cat') print(me ...