博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[leetcode-463-Island Perimeter]
阅读量:6425 次
发布时间:2019-06-23

本文共 1524 字,大约阅读时间需要 5 分钟。

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: 16Explanation: The perimeter is the 16 yellow stripes in the image below:

参考的大牛的思路:

  1. find how many 1 in the map. If without the consideration of surrounding cells, the total perimeter should be the total amount of 1 times 4.
  2. find how many cell walls that connect with both lands. We need to deduct twice of those lines from total perimeter
int islandPerimeter(vector
>& grid) { int count = 0, repeat = 0; for (int i = 0; i < grid.size(); i++) { for (int j = 0; j < grid[i].size(); j++) { if (grid[i][j] == 1) { count++; if (i != 0 && grid[i - 1][j] == 1) repeat++; if (j != 0 && grid[i][j - 1] == 1) repeat++; } } } return 4 * count - repeat * 2; }

参考:

转载于:https://www.cnblogs.com/hellowooorld/p/6842902.html

你可能感兴趣的文章
『翻译』Node.js 调试
查看>>
我的iOS开发之路总结(更新啦~)
查看>>
Java NIO之拥抱Path和Files
查看>>
微信原图泄露的只能是 Exif ,你的隐私不在这!!!
查看>>
微信小程序教学第三章(含视频):小程序中级实战教程:列表篇-页面逻辑处理...
查看>>
页面间通信与数据共享解决方案简析
查看>>
Swift 中 Substrings 与 String
查看>>
作为一个开源软件的作者是一种什么样的感受?
查看>>
移动端适配知识你到底知多少
查看>>
TiDB 在 G7 的实践和未来
查看>>
重新认识javascript对象(三)——原型及原型链
查看>>
小学生学“数学”
查看>>
FastDFS蛋疼的集群和负载均衡(十七)之解决LVS+Keepalived遇到的问题
查看>>
深入剖析Redis系列(二) - Redis哨兵模式与高可用集群
查看>>
Android 用于校验集合参数的小封装
查看>>
iOS混合开发库(GICXMLLayout)七、JavaScript篇
查看>>
instrument 调试 无法指出问题代码 解决
查看>>
理解缓存
查看>>
BAT 经典算法笔试题 —— 磁盘多路归并排序
查看>>
Nginx限制带宽
查看>>