原题链接在这里: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. php curl 例子

    get方式: set_time_limit(0);        $url ='http://xxxxxxxx?pwd=wJ2312&s=1&e=2&d=1&t=asc ...

  2. thinkphp 杂项(3.2.3)

    G('start');//时间描点start php代码.... G('end');//时间描点end echo G('start','end',4).'-----------'; 时间统计echo ...

  3. 【C#】 知乎用户网络爬虫

    目的 由一个种子用户出发,抓取相关的关注者和被关注者,然后再延伸开抓取更多的相关用户,以便后期进行数据分析. 拓扑图 开发工具 编程语言:C# 数据库:SqlServer 2008 R2 程序架构 流 ...

  4. SVN-功能介绍之切换

    当初新建项目IMyYa 提交到svn 目录下file:///H:/svn/truck/IMyYa 现在svn 目录版本库中调整为file:///H:/svn/truck/Win8/IMyYa 与之前不 ...

  5. JavaScript Bind()趣味解答 包懂~~

    首先声明一下,这个解答是从Segmentfault看到的,挺有意思就记录下来.我放到最下面: bind() https://developer.mozilla.org/zh-CN/docs/Web/J ...

  6. 破解加密PDF文件pdfcrack

    破解加密PDF文件pdfcrack   PDF是常见的文档格式.它允许用户设置双重密码来保护文档.第一重是用户密码(user password),当打开PDF文档,输入该密码.第二重是所有者密码(ow ...

  7. Model Validation in ASP.NET Web API

    Model Validation in ASP.NET Web API 原文:http://www.asp.net/web-api/overview/formats-and-model-binding ...

  8. 【转】前端工程筹建NodeJs+gulp+bower

    转自:http://www.myexception.cn/javascript/1781968.html npm nodejs 安装过程中会自动安装npm,nodejs安装程序会在环境变量中添加两个变 ...

  9. Leetcode Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  10. 洛谷 P1026 统计单词个数 Label:dp

    题目描述 给出一个长度不超过200的由小写英文字母组成的字母串(约定;该字串以每行20个字母的方式输入,且保证每行一定为20个).要求将此字母串分成k份(1<k<=40),且每份中包含的单 ...