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:
分析:计算给出图形的周长,不存在孤岛。
思路:统计相邻岛屿数分别为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的更多相关文章
- Leetcode-463 Island Perimeter
#463. Island Perimeter You are given a map in form of a two-dimensional integer grid where 1 represe ...
- LeetCode_463. Island Perimeter
463. Island Perimeter Easy You are given a map in form of a two-dimensional integer grid where 1 rep ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 463. Island Perimeter - LeetCode
Question 463. Island Perimeter Solution 题目大意:给出一个二维数组1表示陆地0表示海,求陆地的周长 思路: 重新构造一张地图grid2即一个二维数组,比原数组大 ...
- [LeetCode] Island Perimeter 岛屿周长
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- 463. Island Perimeter
https://leetcode.com/problems/island-perimeter/ 在一个N×N的矩阵中,N<100,1代表岛,0代表海,岛内没有海,求岛的周长 [[0,1,0,0] ...
- LeetCode Island Perimeter
原题链接在这里:https://leetcode.com/problems/island-perimeter/ 题目: You are given a map in form of a two-dim ...
- 【LeetCode】463. Island Perimeter
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- leetcode算法:Island Perimeter
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
随机推荐
- ajax请求后台,有时收不到返回值的解决办法
昨天下午做项目遇到一个问题,贴出来方便以后翻阅,也给大家个参考. 问题: 具体做的是个文件导入的功能,导入的功能是成功了,但是界面一直得不到返回值,排查了一下午,调试的时候是可以有返回的,但是关掉浏览 ...
- spring 整合Mybatis 错误:Parameter 'items_id' not found. Available parameters are [array]
运行环境:jdk1.7.0_17+tomcat 7 + spring:3.2.0 +mybatis:3.2.7+ eclipse 错误:Parameter 'items_id' not found. ...
- 在Android上仿百度贴吧客户端Loading图标小球
封面 前言 使用百度贴吧客户端的时候发发现加载的小动画挺有意思的,于是自己动手写写看.想学习自定义View以及自定义动画的小伙伴一定不要错过哦. 读者朋友需要有最基本的canvas绘图功底,比如画笔P ...
- Microsoft Visual Studio 打开代码出现乱码解决方案
在用VS编写代码时,文本的字符集可能和编译器的字符集不同,在这种情况下代码会显示出乱码. 解决办法: 在VS的工具->选项里面找到"文本编辑器",勾选“自动检测不带签名的UT ...
- Excel表单的读取与处理 PHPExcel与Apache POI
近日,连续遇到需要对Excel表单内容进行读取的需求.一个是在php环境下,一个是在java环境下.这里简要记录这两种环境,利用第三方提供的函数库对Excel进行处理的方法. d0710 : Fini ...
- 如何在centos7上安装源码包
在我们使用linux的过程中,有很多程序是通过红帽官网给的系统中安装的,但是一般来说,系统更新的速度比较慢,如果这个时候我们又想用最新版的该怎么办呢?总不能一直等系统升级吧╮(╯﹏╰)╭所以,我们可以 ...
- 浏览器console的用法
Leo_wlCnBlogs 自由.创新.研究.探索 Linux/Windows Mono/DotNet [ Open Source .NET Development/ 使用开源工具进行DotNet软件 ...
- 201521123090《Java程序设计》第6周学习总结
本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结. 注1:关键词与内容不求多,但概念之间的联系要清晰,内容覆盖面向对 ...
- 201521123071《java程序设计》第三周学习总结
1. 本周学习总结 这周主要学习了构造函数,类与对象,就是这周事情很多,还没来得及好好复习,所以有很多知识都没有认识透彻.但我会尽力补上的. http://images2015.cnblogs.com ...
- 201521123022 《Java程序设计》 第二周学习总结
1. 本章学习收获 (1)在老师指导下学会如何使用码云管理代码,代码不仅是保存到本地,还需要Push到码云这个"仓库"里. (2)JDK源代码可以为我们的编程提供许多便利之处,应善 ...