【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 ...
随机推荐
- Excel-在整个工作簿中查找/替换
13.在整个工作簿中查找/替换 调范围为:工作簿,默认是工作表:
- 33. Implement strStr()
http://blog.csdn.net/justdoithai/article/details/51287649 理解与分析 Implement strStr() My Submissions Qu ...
- jQuery ajax常用示例
总结一下jQuery ajax常用示例 $.ajax({ type: "post", //类型get,post url: urls, //链接地址 data:{"id&q ...
- 单片机ISP、IAP和ICP几种烧录方式的区别
单片机ISP.IAP和ICP几种烧录方式的区别 玩单片机的都应该听说过这几个词.一直搞不太清楚他们之间的区别.今天查了资料后总结整理如下. ISP:In System Programing,在系统编程 ...
- 从零构建Java项目(Maven+SpringBoot+Git) #02 奥斯丁项目
前两天我说要写个项目来持续迭代,有好多小伙伴都表示支持和鼓励,项目的第一篇这不就来了么~我给项目取了个名字,英文名叫做:austin,中文名叫做:奥斯丁 名字倒没有什么特别的含义,我单纯觉得这个名字好 ...
- Flink(九)【Flink的重启策略】
目录 1.Flink的重启策略 2.重启策略 2.1未开启checkpoint 2.2开启checkpoint 1)不设置重启策略 2)不重启 3)固定延迟重启(默认) 4)失败率重启 3.重启效果演 ...
- Kafka 架构深入
Kafka 工作流程及文件存储机制
- C++中union相关
前两天做阿里笔试遇到一个选择题题目大概是 #include <iostream> #include <stdlib.h> using namespace std; union ...
- jenkins之授权和权限管理
#:创建角色,给角色授权,然后创建用户,将用户加入到角色(前提先安装插件) #:先将之前的卸载掉 #:然后重启服务,在可选插件搜索Role #:装完重启服务 root@ubuntu:~# system ...
- 莫烦python教程学习笔记——保存模型、加载模型的两种方法
# View more python tutorials on my Youtube and Youku channel!!! # Youtube video tutorial: https://ww ...