463. Island Perimeter - LeetCode
Question

Solution
题目大意:给出一个二维数组1表示陆地0表示海,求陆地的周长
思路:
重新构造一张地图grid2即一个二维数组,比原数组大一圈,即长宽都大2
一个点在原地图坐标是(i,j),那么在重新构造的坐标就是(i+1,j+1)
遍历原地图,如果一是陆地,就遍历这个点的周围是否是海,如果是海线周长就加1
Java实现:
public int islandPerimeter(int[][] grid) {
int total = 0;
int[][] grid2 = new int[grid.length+2][grid[0].length+2];
for (int i=0; i<grid.length; i++) {
for (int j=0; j<grid[0].length; j++) {
if (grid[i][j] == 1) {
grid2[i+1][j+1] = 1;
}
}
}
for (int i=0; i<grid.length; i++) {
for (int j=0; j<grid[i].length; j++) {
int x = i+1;
int y = j+1;
if (grid2[x][y] == 0) continue;
// left
total += grid2[x-1][y]==1?0:1;
// right
total += grid2[x+1][y]==1?0:1;
// top
total += grid2[x][y-1]==1?0:1;
// bottom
total += grid2[x][y+1]==1?0:1;
}
}
return total;
}
463. Island Perimeter - LeetCode的更多相关文章
- 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] 463. Island Perimeter 岛的周长
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- 【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 represen ...
- 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 463. Island Perimeter岛屿的周长 (C++)
题目: You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 repr ...
- [LeetCode&Python] Problem 463. Island Perimeter
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- LeetCode - 463. Island Perimeter - O(MN)- (C++) - 解题报告
原题 原题链接 You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 ...
- LeetCode: 463 Island Perimeter(easy)
题目: You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 repr ...
随机推荐
- BMZCTF SDNISC2020_过去和现在
SDNISC2020_过去和现在 打开附件就一张图片 根据题意感觉是图片中隐藏了什么信息 使用binwalk -e分离这里foremost不行 三个文件查看在第一个中发现flag
- (stm32f103学习总结)—RTC独立定时器—实时时钟实验
一.STM32F1 RTC介绍 1.1 RTC简介 STM32 的实时时钟( RTC)是一个独立的定时器. STM32 的 RTC 模 块拥有一组连续计数的计数器,在相应软件配置下,可提供时钟日历的 ...
- 编译器警告c4996
由于编译器的原因(我用的是vs 2012),我们写程序时有时候会遇到编译器给出的警告,如: warning C4996: 'fopen': This function or variable may ...
- 转载:介绍AD另外一种奇葩的多通道复用的方法
原文链接:http://www.eda365.com/forum.php?_dsign=74fe4957&mod=viewthread&page=1&tid=110710 在设 ...
- 检查浏览器支持Webp
什么是Webp? Webp 是一种支持有损压缩和无损压缩的图片文件格式,派生自图像编码格式 VP8.根据 Google 的测试,无损压缩后的 WebP 比 PNG 文件少了 45% 的文件大小,即使这 ...
- Java/C++实现观察者模式--股票价格
当股票的价格上涨或下降5%时,会通知持有该股票的股民,当股民听到价格上涨的消息时会买股票,当价格下降时会大哭一场. 类图: Java代码: public class Investor implemen ...
- Python网络爬虫 - 爬取中证网银行相关信息
最终版:07_中证网(Plus -Pro).py # coding=utf-8 import requests from bs4 import BeautifulSoup import io impo ...
- 【版本2020.03】使用idea导入maven项目
心得1:不同版本的idea,一些选项的名称稍微有点不同,比如以前导入项目的选项名称都是import Project,但是我使用的版本是2020.03 导入项目的名称是 import Settings ...
- GitHub Pages + Hexo搭建个人博客网站-github风格-采坑记录
目录 1.本机安装nodejs 2.github上创建仓库 3.安装hexo 4.hexo主题 5.配置主题 6.添加文章 7.使用分类和标签 8.增加文章目录 9.推送github 使用github ...
- Java设计模式——抽象工厂模式
抽象工厂模式也是创建模式,可以把它理解成创建工厂的工厂,这种模式也是我们经常使用的.在抽象工厂中的接口是用来创建工厂的,每个生成的工厂又都可以按照工厂模式创建其他对象. 举例说明: 创建Shape接口 ...