【LeetCode】Island Perimeter 解题报告
【LeetCode】Island Perimeter 解题报告
[LeetCode]
https://leetcode.com/problems/island-perimeter/
- Total Accepted: 16143
- Total Submissions: 28552
- Difficulty: Easy
Question
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:
Ways
有点类似小学火柴棍的问题。我最初的想法是统计一的个数,找规律,试验了几次之后发现貌似是1的个数*2+2,但是这个是错误的。比如最角落的1如果被周围的1所包围的话,实际上是不贡献边的。
后来想到,应该把边角的被包围的1去掉,中间的被四个1包围的1也给去掉,剩余的1的个数*4.这样很麻烦,因为判断处于边角位置的1就要写4个If,判断被包围的1也要判断四次。
看到top的答案我才明白,可以统计1的个数*4 再减去 与其有交集的1的个数*2.为了防止减去的边不被重复计算,所以,只判断右边和下边的1.代码如下。
public class Solution {
public int islandPerimeter(int[][] grid) {
int count=0;
int neighbours=0;
for(int i=0; i<grid.length; i++){
for(int j=0 ;j<grid[i].length; j++){
if(grid[i][j]==1){
count++;// count islands
if(j<grid[i].length-1 && grid[i][j+1]==1)
neighbours++;// count right neighbours
if(i<grid.length-1 && grid[i+1][j]==1)
neighbours++;// count down neighbours
}
}
}
return 4*count-2*neighbours;
}
}
果然这种方法判断的次数要少一点。
Date
2017 年 1 月 6 日
【LeetCode】Island Perimeter 解题报告的更多相关文章
- 【LeetCode】463. Island Perimeter 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 减去相交部分 参考资料 日期 题目地址:https: ...
- LeetCode 463 Island Perimeter 解题报告
题目要求 You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 rep ...
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- LeetCode - Course Schedule 解题报告
以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- [LeetCode] 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
原题链接在这里:https://leetcode.com/problems/island-perimeter/ 题目: You are given a map in form of a two-dim ...
- LeetCode: Permutation Sequence 解题报告
Permutation Sequence https://oj.leetcode.com/problems/permutation-sequence/ The set [1,2,3,…,n] cont ...
随机推荐
- 【6】蛋白质组学鉴定定量软件之MaxQuant
目录 1.简介 2.下载安装 3.配置与运行 4.结果 5.Perseus后处理 6.小结 1.简介 2016年,德国马普所的Cox和蛋白质组学领域巨擘Matthias Mann合作开发了MaxQua ...
- vim——批量缩进
批量缩进 第一种 按esc,退出编辑模式,到命令模式,并在英语输入法下输入":" 将所要批量缩进的行号写上,按照格式:"行号1,行号2>"输入命令,如要将 ...
- C++20协程实例:携程化的IOCP服务端/客户端
VC支持协程已经有一段时间了,之前一直想不明白协程的意义在哪里,前几天拉屎的时候突然灵光一闪: 以下是伪代码: task server() { for (;;) { sock_context s = ...
- 【NetCore】RabbitMQ 封装
RabbitMQ 封装 代码 https://gitee.com/wosperry/wosperry-rabbit-mqtest/tree/master 参考Abp事件总线的用法,对拷贝的Demo进行 ...
- 用JS实现方块碰撞
首先我们应用上次的内容--方块拖拽,利用方块拖拽来让两个方块进行碰撞. 我们可以先定义两个正方形方块,红色的div1,绿色的div2,我们来实现当div1碰撞div2时div2的颜色变为黄色 HTML ...
- C++中Try Catch中的继承
1.C++中Try Catch简介:我们编译运行程序出错的时候,编译器就会抛出异常.抛出异常要比终止程序灵活许多. 而C++异常是指在程序运行时发生的反常行为,这些行为超出了函数正常功能的范围.当程序 ...
- hadoop Sort排序
1 public int getPartition(IntWritable key,IntWritable value,int numPartitions){ 2 int Maxnumber = 12 ...
- CSS3新增特性\HTML标签类型
RGBA:透明度 作用: 设置透明度(R G B A) opacity:不透明度 文字也会被设置不透明度 圆角 border-radius:圆角{左上角,右上角.. ...
- Dubbo声明式缓存
为了进一步提高消费者对用户的响应速度,减轻提供者的压力,Dubbo提供了基于结果的声明式缓存.该缓存是基于消费者端的,所以使用很简单,只需修改消费者配置文件,与提供者无关 一.创建消费者07-cons ...
- springmvc中拦截器的定义和配置
package com.hope.interceptor;import org.springframework.lang.Nullable;import org.springframework.web ...