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. 用antlr4来实现《按编译原理的思路设计的一个计算器》中的计算器

    上次在公司内部讲<词法分析——使用正则文法>是一次失败的尝试——上午有十几个人在场,下午就只来了四个听众. 本来我还在构思如何来讲“语法分析”的知识呢,但现在看来已不太可能. 这个课程没有 ...

  2. VS2015 安装包缺失(联网安装失败)问题解决

    Win7 x86 测试可行 *  如果前面有尝试过安装不成功, 一定要用卸载程序删除已安装的部分,否则会出乱子. 1. 或者是用虚拟光驱加载ISO, 或者是解压到硬盘上, 都没有关系. 2. 用管理员 ...

  3. Qt杂记——布局、信号与槽等

    1.QHBoxLayout布局设置拉伸: ui->TopLayout->setStretch(,); //left ui->TopLayout->setStretch(,); ...

  4. Ubuntu-Python2.7安装 scipy,numpy,matplotlib

    sudo apt-get install python-scipy sudo apt-get install python-numpy sudo apt-get install python-matp ...

  5. python队列的实现

    队列是一种抽象数据结构,具有以下特点: (1)具有先进先出的特性(FIFO) (2)拥有两种基本操作,即加入和删除,而且使用front和rear两个指针来分别指向队列的前端和末尾. 队列的基本操作 c ...

  6. oracle 备份/恢复

    oracle备份是为了有问题能够快速恢复:

  7. 【Redis】二、Redis高级特性

    (三) Redis高级特性   前面我们介绍了Redis的五种基本的数据类型,灵活运用这五种数据类型是使用Redis的基础,除此之外,Redis还有一些特性,掌握这些特性能对Redis有进一步的了解, ...

  8. java导出word的6种方式(转发)

    来自: http://www.cnblogs.com/lcngu/p/5247179.html 最近做的项目,需要将一些信息导出到word中.在网上找了好多解决方案,现在将这几天的总结分享一下. 目前 ...

  9. linux命令 info

    info命令是Linux下info格式的帮助指令. 就内容来说,info页面比man page编写得要更好.更容易理解,也更友好,但man page使用起来确实要更容易得多.一个man page只有一 ...

  10. react入门----基础语法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...