Question

463. Island Perimeter

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的更多相关文章

  1. LeetCode 463. Island Perimeter (岛的周长)

    You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...

  2. [LeetCode] 463. Island Perimeter 岛的周长

    You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...

  3. 【LeetCode】463. Island Perimeter 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 减去相交部分 参考资料 日期 题目地址:https: ...

  4. 【LeetCode】463. Island Perimeter

    You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...

  5. LeetCode 463 Island Perimeter 解题报告

    题目要求 You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 rep ...

  6. 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 ...

  7. [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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 使用Javascript获取剪贴板图片的DataURL

    最近写博客需要插入一些截图,想着用DataURL会方便点,于是需要一个把图片转成DataURL的工具.搜索一番后发现这个功能用HTML就能实现,通过paste事件. 先尝试在Chrome上实现,Chr ...

  2. 关于Css的垂直居中的一些方法

    前两种方法称为大致居中,一般误差随高度的减小而减小,不过一般来说不怎么看得出来,除非你用javascript调用offsetTop来查看.不然没有强迫症的比较难看出来.但是兼容性很好,尤其是table ...

  3. HTML5存储方式

    由于之前在参加面试或者笔试的过程中经常会被问到HTML5存储的内容,包括它们之间的区别.特征和应用范围,所以看到一篇介绍不错的文章,把里面的个人觉得适合我的内容按照自己的理解总结如下.方便以后忘记了进 ...

  4. 深入理解ES6(二)(解构赋值)

    变量的解构赋值 (1) 数组的解构赋值 1.基本用法 ES6 允许按照一定模式,从数组和对象中提取值,对变量进行赋值,这被称为解构(Destructuring ). 只要等号两边的模式相同,左边的变量 ...

  5. 前端面试题整理——普通函数和new函数

    下列代码的输出值: function A() { console.log(1) } function fn() { A = function () { console.log(2) } return ...

  6. python-爬楼梯

    [题目描述] 假设一段楼梯共n(n>1)个台阶,小朋友一步最多能上3个台阶,那么小朋友上这段楼梯一共有多少种方法. [练习要求]请给出源代码程序和运行测试结果,源代码程序要求添加必要的注释. [ ...

  7. 集合框架基础三——Map

    Map接口  * 将键映射到值的对象  * 一个映射不能包含重复的键  * 每个键最多只能映射到一个值 Map接口和Collection接口的不同 * Map是双列的,Collection是单列的 * ...

  8. SpringMVC基于注解开发的步骤

    基于xml配置 .1准备好以下相关jar包 .2创建Maven项目使用骨架  (这里选择第二个以webapp结尾的非第一个) 给项目起个名字 这里可以更改maven本地仓库(依赖包所存放的地方)的路径 ...

  9. [强网杯2019]upload buuoj

    提示:重点在这,可节省大部分时间 扫描后台 发现www.tar.gz备份文件. 这平台有429[太多请求限制]防护.dirsearch扫描一堆429.于是用了最笨的方法. 文件上传 先注册个账号 注册 ...

  10. Linux小工具的应用,grep,sort,wc,cut

    小工具的使用: 1.管道(|):连接多个命令的工具,进程之间通讯的一种方式 用法:命令1 | 命令2 | 命令3....2.grep工具:行过滤,打印出的结果一行一行的 用法:grep options ...