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: ...
随机推荐
- C++ "#"的作用和使用方法
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/48879093 1 #和##的作用和使用 ...
- ListView优化总结(二)--Android
3.使用Activity和Delegate与适配器交互 这个内容是从书里看到的,通过托付模式帮助开发人员把全部的业务逻辑从适配器中移到Activity中. 以下是加入电话号码的样例,列表中每一行都有一 ...
- eclipse下Android工程名称的修改方法
eclipse下Android工程名称的修改方法 对于已经建立的工程,如果发现原来的工程名不合适,此时若想彻底更改工程名,需要三个步骤: 1.更改工程名 选中工程名,右键-->Refactor- ...
- Web性能测试工具:Siege安装&使用简介
在Web性能测试工具中,siege是比较热门和常见的,它有安装简单,使用简单,测试报告详细的特点. 并且可以在文本中预定义一系列待测试url模拟,并可设定一定并发量下持续指定时间or测试进行测试. 比 ...
- xpath 轴,节点之间的关系
http://www.w3school.com.cn/xpath/xpath_axes.asp http://www.freeformatter.com/xpath-tester.html 测试 轴可 ...
- Nmap扫描教程之基础扫描具体解释
Nmap扫描教程之基础扫描具体解释 Nmap扫描基础扫描 当用户对Nmap工具了解后,就可以使用该工具实施扫描.通过上一章的介绍,用户可知Nmap工具能够分别对主机.port.版本号.操作系统等实施扫 ...
- 【百度之星初赛A】路径交 LCA+线段树
[百度之星初赛A]路径交 Problem Description 给定一棵n个点的树,以及m条路径,每次询问第L条到第R条路径的交集部分的长度(如果一条边同时出现在2条路径上,那么它属于路径的交集). ...
- 九度OJ 1115:数字求和 (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2396 解决:1507 题目描述: 给定一个正整数a,以及另外的5个正整数,问题是:这5个整数中,小于a的整数的和是多少? 输入: 输入一行 ...
- 在VC++空工程中使用MFC类,采用Unicode字符集后,运行工程程序报错的解决方案
创建一个VC++空工程,将Project Properties->General->Use of MFC改为Use MFC in a Shared DLL 新建一个源文件,内容如下 #in ...
- meteor ---快速启动meteor和 mongodb 方法--MAC
c:~ lsg$ cat .bash_profile c:~ lsg$ vim .bash_profile --- 修改这个文件 按i 修改文件 shift+Z+Z 保存修改内容 添加如下代码 exp ...