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. chrome开发工具指南(十)

    检查和删除 Cookie 从 Application 面板检查和删除 Cookie. TL;DR 查看与 Cookie 有关的详细信息,例如名称.值.网域和大小,等等. 删除单个 Cookie.选定网 ...

  2. FTP的主动和被动模式详解

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp25 主动模式FTP与被动模式FTP该如何选择 一.主动模式的实现与特点. ...

  3. Servlet和JSP生命周期概述

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt374 Servlet生命周期分为三个阶段: 1,初始化阶段  调用init( ...

  4. Javascript学习日志(三):闭包

    说实话,前面一节的原型和原型链在当初学的时候并没有很头疼,对着高级编程第三版撸了几遍就理解透了,闭包这一节真的挺头疼的,很惭愧,看了差不多十来遍吧,还翻看了网上的其他博客和解释文档,五花八门的表达方式 ...

  5. JavaScript学习日志(一):变量,作用域和内存问题

    一,变量分为两种类型:基本类型值和引用类型值,基本类型包括:Undefined, String, Boolean, Null, Number,我们无法给基本类型值添加属性: 二,复制变量值的时候,如果 ...

  6. MongoDB学习之路(一)

    NoSQL简介 NoSQL(Not Only SQL),意为"不仅仅是SQL" 关系型数据库遵循ACID规则 1. A(Atomicity)原子性 指的是事务里的所有操作要么全部做 ...

  7. 集美大学网络1413第七次作业成绩(团队三) --需求改进&系统设计

    题目 团队作业3--需求改进&系统设计 团队作业3成绩  团队/分值 TD BZ GJ CJ SI WBS GS JG DB SS SJ CS DC 总分  1 0.25 0.75 1 0.5 ...

  8. 201521123052《Java程序设计》第2周学习总结

    1. 本周学习总结 String类一些用法 学习Array类 使用枚举 使用eclipse关联JDK源代码 使用码云管理云代码 2.书面作业 1.使用Eclipse关联jdk源代码,并查看String ...

  9. 201521123052 《Java程序设计》 第14周学习总结

    1. 本周学习总结 2. 书面作业 1. MySQL数据库基本操作 建立数据库,将自己的姓名.学号作为一条记录插入.(截图,需出现自己的学号.姓名) 在自己建立的数据库上执行常见SQL语句(截图) - ...

  10. java课程设计---彩票销售管理系统

    彩票购买销售系统 1.项目git地址 https://git.oschina.net/fenm/lotterry.git 部分项目地址提交截图 项目主要功能图 团队博客链接 http://www.cn ...