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 ...
随机推荐
- jmeter日记
1.不要像loadrunner一样使用集合点,jmeter不需要 2.性能测试 线程组设置的时候 永远 3.启动jmeter之后,打开log 4.分布式的话 tps是多台电脑的tps之和 自动加好了的 ...
- < 软件工程 第一次作业 >
自我介绍: 老师好! 我叫李智强,专业是计算机科学与技术,我自己也喜欢这个专业,然后这是我第一次用博客写自我介绍,可能会写的有点不好,还请包涵. 课程期望和目标: 第一次上课,听着老师说我们可能会做很 ...
- 定时调度框架:Quartz.net
Quartz.net相关概念 经常出现场景:定时轮询数据库同步,定时邮件通知,定时处理数据等 Scheduler (计划者或调度器) Job (工作对象):将要定时执行的任务代码写到实现Ijob接口的 ...
- [C#] 分布式ID自增算法 Snowflake
最近在尝试EF的多数据库移植,但是原始项目中主键用的Sqlserver的GUID.MySQL没法移植了. 其实发现GUID也没法保证数据的递增性,又不太想使用int递增主键,就开始探索别的ID形式. ...
- Spring事务管理(一)
对于Spring相信很多做web开发的小活动一定不陌生,Spring中我们经常谈到的就是IOC和AOP,但是对于Spring的事务管理,相信大家一定也很感兴趣,今天我们就探讨一下Spring中的事务管 ...
- Apache配置网站根目录
Apache是世界使用排名第一的Web服务器软件.它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一. 在安装 Apache 时,系统会给定一 ...
- 设计模式-单体模式(C++)
设计模式-单体模式 单体模式在使用非常方便,适合于单一的对象,例如全局对象的抽象使用. 需要注意的是单体模式不可继承 // 实现 Singleton.h #ifndef __SINGLETON_H__ ...
- 【Beta】阶段 第五次Daily Scrum Meeting
每日任务 1.本次会议为第五次 Meeting会议: 2.本次会议在周五下午15:35,在禹洲楼召开,召开本次会议为10分钟. 一.今日站立式会议照片 二.每个人的工作 (有work item 的ID ...
- 个人作业2-英语学习案例app分析
第一部分 调研, 评测 (软件的bug,功能评测,黑箱测试, 第8章 用户调研, 12 章 软件的用户体验) 下载并使用,描述最简单直观的个人第一次上手体验. ①个人感觉还不错,词典的首页页面挺好看的 ...
- 201521123060 《Java程序设计》第5周学习总结
1.本周学习总结 2.书面作业 Q1.代码阅读:Child压缩包内源代码 1.1 com.parent包中Child.java文件能否编译通过?哪句会出现错误?试改正该错误.并分析输出结果. 答:不能 ...