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. 廖雪峰 Git教程学习笔记 原文 http://www.liaoxuefeng.com/

    一 .集中式与分布式        先说集中式版本控制系统,版本库是集中存放在中央服务器的,而干活的时候,用的都是自己的电脑,所以要先从中央服务器取得最新的版本,然后开始干活,干完活了,再把自己的活推 ...

  2. ThinkPHP系统流程

    1.用户通过入口文件访问控制器2.控制器从模型层中提取数据3.控制器将数据返回模板页面

  3. MSSQLSERVER_3176

    本文档已存档,并且将不进行维护. MSSQLSERVER_3176                 SQL Server 2014                    SQL Server 2012 ...

  4. 生成count个[0-n)不重复的随机数

    代码来自:https://www.cnblogs.com/ningvsban/p/3590722.html,感觉实现的方式不错(做了一点小小修改) public static ArrayList ge ...

  5. 03Oracle Database 物理结构,逻辑结构

    Oracle Database 物理结构,逻辑结构 Oracle Database 物理结构 数据文件 Oracle数据库有一个或多个物理的数据文件.数据库的数据文件包含全部数据库数据.逻辑数据物理地 ...

  6. datagrid总条数

    1.getData var data=$("#dg").datagrid("getData");alert('总数据量:' + data.total)//注意你 ...

  7. BZOJ 2223: [Coci 2009]PATULJCI 主席树

    Code: #include<bits/stdc++.h> #define maxn 300001 #define mid ((l+r)>>1) using namespace ...

  8. Luogu P3110 [USACO14DEC]驮运Piggy Back

    解题思路 看到下面很多人都在说什么遇到了之后要不要背着走,其实根本不需要,同样的我也是跑了三遍$SPFA$,求出了以$1$为起点到个点的$dist$,和以$2$为起点到个点的$dist$,还有以$n$ ...

  9. 日本語 IME输入法(Microsoft 输入法)切换问题

    平假名 与 片假名之间的切换 按住 Ctrl + Caps Lock(平假名) 按住 Alt + Caps Lock(片假名) 另外:语言之间的切换 Alt + Shift 键 / Windows + ...

  10. [Algorithm] 3. Digit Counts

    Description Count the number of k's between 0 and n. k can be 0 - 9. Example if n = 12, k = 1 in [0, ...