原题链接在这里:https://leetcode.com/problems/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:

题解:

When there is an island, we want to accumlate 4 to the res.

But if there is shared board with left and top, we need to minus board count * 2 from res.

Time Complexity: O(m*n), m = grid.length, n = grid[0].length.

Space: O(1).

AC Java:

 class Solution {
public int islandPerimeter(int[][] grid) {
if(grid == null || grid.length == 0 || grid[0].length == 0){
return 0;
} int m = grid.length;
int n = grid[0].length;
int res = 0; for(int i = 0; i<m; i++){
for(int j = 0; j<n; j++){
if(grid[i][j] == 1){
int count = 0;
if(i > 0 && grid[i - 1][j] == 1){
count++;
} if(j > 0 && grid[i][j - 1] == 1){
count++;
} res = res + 4 - 2 * count;
}
}
} return res;
}
}

类似Max Area of IslandColoring A Border.

LeetCode Island Perimeter的更多相关文章

  1. [LeetCode] Island Perimeter 岛屿周长

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

  2. 【LeetCode】Island Perimeter 解题报告

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

  3. 463. Island Perimeter - LeetCode

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

  4. LeetCode_463. Island Perimeter

    463. Island Perimeter Easy You are given a map in form of a two-dimensional integer grid where 1 rep ...

  5. Leetcode-463 Island Perimeter

    #463. Island Perimeter You are given a map in form of a two-dimensional integer grid where 1 represe ...

  6. LeetCode 463. Island Perimeter (岛的周长)

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

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

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

  8. 【LeetCode】463. Island Perimeter 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 减去相交部分 参考资料 日期 题目地址:https: ...

  9. 【LeetCode】463. Island Perimeter

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

随机推荐

  1. Lamp和Lnmp环境搭建

    一.安装Lamp wget -c http://soft.vpser.net/lnmp/lnmp1.2-full.tar.gz && tar zxf lnmp1.2-full.tar. ...

  2. 开发Visual Studio 插件

    新建项目,在其他项目类型中找到扩展性,然后新建Visual Studio外接程序,VS中没有这个要安装SDK,http://yunpan.cn/cd27eNczKREwd (提取码:ab29)去下载 ...

  3. win7修改护眼色

    一. 手动修改 记得qq管家有一个功能就是护眼模式 那如何不通过第三方软件修复系统护眼色呢.百度后我在此记录下: 1.在桌面右键单击,选择“个性化”,在下面找到如图所示,点击进去 2.高级外观设置 3 ...

  4. Oracle资源管理器介绍(一)

        数据库资源管理器通过控制数据库内部的执行调度来控制资源在各个会话之间的分布.通过控制所要运行的会话以及会话运行的时间长度,数据库资源管理器可以确保资源分布与计划指令相匹配,因此也符合业务目标. ...

  5. C# 执行文件的根目录 (转)

    1.取得控制台应用程序的根目录方法 方法1.Environment.CurrentDirectory 取得或设置当前工作目录的完整限定路径方法2.AppDomain.CurrentDomain.Bas ...

  6. 在ASP.NET MVC项目中使用React

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:最近在开发钉钉的微应用,考虑到性能和UI库的支持,遂采用了React来开发前端. 目前 ...

  7. java基础(二)

    1.equals()方法是可以重写的,因此说equals方法是判断两个对象的值是否相等是不完全正确的,当重写方法时可以定义自己的相等规则. 2.Java中的类可以包含六中元素;属性.方法.代码块.内部 ...

  8. linux学习笔记-(1)-安装

    学习的第一部,当然是寻找学习资料,如今的网络如此发达,只要下点功夫,基本上能在网上找到一切自己所需要的东西,而且还是免费滴哟! ---------------------分割线------------ ...

  9. CodeForces 551E 分块

    题目链接:http://codeforces.com/problemset/problem/551/E 题意:给定一个长度为N的序列. 有2个操作 1 l r v:序列第l项到第r项加v(区间加), ...

  10. AMR 转mp3 失败

    private void changeToMp3(String sourcePath) { File source = new File(sourcePath); String mp3TargetPa ...