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:

分析:计算给出图形的周长,不存在孤岛。

思路:统计相邻岛屿数分别为0,1,2,3的个数。计算公式:s0*4+s1*3+s2*2+s4*1

JAVA CODE:

class Solution {
public int islandPerimeter(int[][] grid) {
int s0 = 0, s1 = 0, s2 = 0, s3 = 0;
for(int i = 0; i < grid.length; i++){
for(int j = 0; j < grid[0].length; j++){
int m = grid[i][j];
if(m == 0)
continue;
int s = 0;
if(i > 0 && grid[i - 1][j] == 1)
s++;
if(i < grid.length - 1 && grid[i + 1][j] == 1)
s++;
if(j > 0 && grid[i][j - 1] == 1)
s++;
if(j < grid[0].length - 1 && grid[i][j + 1] == 1)
s++;
if(s == 0)
s0++;
if(s == 1)
s1++;
if(s == 2)
s2++;
if(s == 3)
s3++;
}
}
return s0 * 4 + s1 * 3 + s2 * 2 + s3 * 1;
}
}

Island Perimeter的更多相关文章

  1. Leetcode-463 Island Perimeter

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

  2. LeetCode_463. Island Perimeter

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

  3. 【LeetCode】Island Perimeter 解题报告

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

  4. 463. Island Perimeter - LeetCode

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

  5. [LeetCode] Island Perimeter 岛屿周长

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

  6. 463. Island Perimeter

    https://leetcode.com/problems/island-perimeter/ 在一个N×N的矩阵中,N<100,1代表岛,0代表海,岛内没有海,求岛的周长 [[0,1,0,0] ...

  7. LeetCode Island Perimeter

    原题链接在这里:https://leetcode.com/problems/island-perimeter/ 题目: You are given a map in form of a two-dim ...

  8. 【LeetCode】463. Island Perimeter

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

  9. leetcode算法:Island Perimeter

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

随机推荐

  1. Bootstrap-table使用记录(转)

    HTML代码 /*index.cshtml*/ @section styles{ <style> .main { margin-top:20px; } .modal-body .form- ...

  2. JavaScript实现隔行换颜色

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  3. SQL菜鸟学习札记(一)

    刚开始学SQL,从最基础的语句开始写,用一个LOL数据库做实验.目前使用的工具是MySQL Workbench,感觉比较顺手,界面没花多久时间就读懂的差不多了,所以目前就使用这个工具来做SQL的学习了 ...

  4. apache如何设置缓存

    基本介绍 httpd是一个比较经典的web服务器,也就是静态资源服务器,主要用来服务于一些静态的文件,例如css,js,html等文件,所谓的静态文件,也就是不需要通过服务器进行运行的文件. 在使用静 ...

  5. vmware 遇到 “无法打开内核设备 \\.\Global\vmx86” 解决

    问题描述:vmware没有正常关闭,再次打开使用时蓝屏,在安全模式下再次打开不会蓝屏,但提示"无法打开内核设备 \.\Global\vmx86: 系统找不到指定的文件,你想要安装VMware ...

  6. (转)C语言malloc()与free()的使用

    如何使用 malloc 函数 本文为转载内容,原文地址请点击 不要莫名其妙,其实上面这段小小的对话,就是malloc的使用过程.malloc是一个函数,专门用来从堆上分配内存.使用malloc函数需要 ...

  7. 201521123076 《Java程序设计》第7周学习总结

    1. 本周学习总结 2. 书面作业 Q1.ArrayList代码分析 1.1 解释ArrayList的contains源代码 A:先上源代码: public boolean contains(Obje ...

  8. 201521123066《Java程序设计》第七周学习总结

    第7周-集合 本周学习总结 其他注意点: (1)List主要特征: 其元素以线性方式存储 集合中允许存放重复对象 (2)Set中的对象: 不按特定方式排序 没有重复对象 Set最多有一个null元素 ...

  9. 201521123117 《Java程序设计》第3周学习总结

    1. 本周学习总结: 2. 书面作业 Q1:代码阅读 public class Test1 { private int i = 1;//这行不能修改 private static int j = 2; ...

  10. 戴建钊 201521123023《Java程序设计》第1周学习总结

    1. 本周学习总结 Java 不同于C语言面向过程的编程思想,它是一种面向对象的编程方式,其在语句语法上与C++类似,因此刚开始可能比较容易理解 2. 书面作业 为什么java程序可以跨平台运行?执行 ...