LeetCode: 463 Island Perimeter(easy)
题目:
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:
代码:
循环遍历就好了~
class Solution {
public:
int islandPerimeter(vector<vector<int>>& grid) {
int result = ;
int n = grid.size();
int m = grid[].size();
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(grid[i][j] == ){
if(i== || grid[i-][j] == ) result++;
if(i==n- || grid[i+][j] == ) result++;
if(j== || grid[i][j-] == ) result++;
if(j==m- || grid[i][j+] == ) result++;
}
}
}
return result;
}
};
LeetCode: 463 Island Perimeter(easy)的更多相关文章
- 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 解题报告
题目要求 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 - 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 ...
- 3. leetcode 463 Island Perimeter
思路:设原始周长为4*节点数,每当出现一次相邻的情况,原始周长会减2.
- 463. Island Perimeter - LeetCode
Question 463. Island Perimeter Solution 题目大意:给出一个二维数组1表示陆地0表示海,求陆地的周长 思路: 重新构造一张地图grid2即一个二维数组,比原数组大 ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】463. Island Perimeter 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 减去相交部分 参考资料 日期 题目地址:https: ...
随机推荐
- UNIX网络编程卷1 时间获取程序client TCP 使用非堵塞connect
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie 1.当在一个非堵塞的 TCP 套接字(可使用 fcntl 把套接字变成非堵塞的)上调用 co ...
- Oracle学习(十三):闪回
1.知识点:能够对比以下的录屏进行阅读 SQL> --1. 错误地删除了记录 SQL> --2. 错误地删除了表 SQL> --3. 查询历史记录 SQL> --4. 怎样撤销 ...
- 开源服务器监控工具 — JavaMelody 类 jvm 内在性能(转)
开源服务器监控工具 — JavaMelody JavaMelody它能够监测Java或Java EE应用程序服务器,并以图表的方式显示:Java内存和Java CPU使用情况,用户Sessio ...
- MongoDB 学习三
这章我们学习MongoDB的查询操作. Introduction to find find方法用于执行MongoDB的查询操作.它返回collecion中的documents子集,没有添加参数的话它将 ...
- BAT网络运维常见面试题目总结
BAT常见面试题目总结 Author:Danbo 2015-7-11 TCP/IP详解鸟哥Linux的书网络安全ping的原理make的过程文件有哪些类型各种Linux发行版的区别.有关suid的作用 ...
- DNS常见攻击与防范
DNS常见攻击与防范 转自:http://www.williamlong.info/archives/3813.html 日期:2015-7-10 随着网络的逐步普及,网络安全已成为INTERNET路 ...
- 2016 Al-Baath University Training Camp Contest-1 I. March Rain —— 二分
题目链接:http://codeforces.com/problemset/gymProblem/101028/I I. March Rain time limit per test 2 second ...
- avf_showspectrum.c:112: undefined reference to `av_rdft_end
下面还有一堆错,是由于ffmpeg库没编好,重新编好即可
- cannot find -lbz2 解决方法
sudo yum install -y bzip2* 或者sudo apt-get install bzip2* 还是报错就找到libbz2.so.1,建立连接或者复制到 /usr/lib/libbz ...
- html5--3.18 新增的output元素
html5--3.18 新增的output元素 学习要点 了解output元素的用法 output元素:数据的输出 output元素是HTML5新增的元素,用来设置不同数据的输出 output元素的输 ...