【LeetCode】463. Island Perimeter
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.
[[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:

题意:一个二维数组中,所有的1组成一个岛,求岛的周长
解题思路:
感觉和419. Battleships in a Board这个题很像,遍历整个数组,考虑每个值上面和左边的值是否为1,是否相接
周长=块数*4-相接个数*2
int islandPerimeter(int** grid, int gridRowSize, int gridColSize) {
int i,j;
int count=,ad=;
for(i=;i<gridRowSize;i++)
{
for(j=;j<gridColSize;j++)
{
if(grid[i][j])
{
count++;
if(==i&&j>)
{
if(grid[i][j-])
ad++;
}
else
if(i!=&&==j)
{
if(grid[i-][j])
ad++;
}
else
if(i!=&&j>)
{
if(grid[i][j-])
ad++;
if(grid[i-][j])
ad++;
}
}
}
}
return *count-*ad;
}
【LeetCode】463. Island Perimeter的更多相关文章
- 【LeetCode】463. Island Perimeter 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 减去相交部分 参考资料 日期 题目地址:https: ...
- 【leetcode】 463. Island Perimeter
题目: 以二维数组形式表示坐标岛屿,求边长. 例子: [[0,1,0,0], [1,1,1,0], [0,1,0,0], [1,1,0,0]] Answer: 16 Explanation: The ...
- 【LeetCode】976. Largest Perimeter Triangle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...
- 【leetcode】976. Largest Perimeter Triangle
题目如下: Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 463. Island Perimeter - LeetCode
Question 463. Island Perimeter Solution 题目大意:给出一个二维数组1表示陆地0表示海,求陆地的周长 思路: 重新构造一张地图grid2即一个二维数组,比原数组大 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
随机推荐
- 【.NET-EF】Entity Framework学习笔记1 - VS2013没有EF的解决方法
解决方法:我本来也没有,百度了一下,在C:\ProgramData\Package Cache\{08AEF86A-1956-4846-B906-B01350E96E30}v12.0.20912.0\ ...
- scapy流量嗅探简单使用
官方文档:http://scrapy-chs.readthedocs.io/zh_CN/latest/index.html 参考链接:http://blog.csdn.net/Jeanphorn/ar ...
- Sipdroid实现SIP(六): SIP中的请求超时和重传
目录 一. Sipdroid的请求超时和重传 二. SIP中超时和重传的定义 三. RFC中超时和重传的定义 一. Sipdroid的请求超时和重传 Sipdroid实现SIP协议栈系列, 之前的文章 ...
- elasticsearch+spark+hbase 整合
1.用到的maven依赖 <dependency> <groupId>org.apache.spark</groupId> <artifactId>sp ...
- ajax无法跳转页面的问题,
将return true去掉!
- pyhon的数据类型
1.数字 整型和浮点型 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2147483648-2147483647在64位系统上,整数的位数为64位,取值范围为-2** ...
- USACO 3.3 Riding the Fences
Riding the Fences Farmer John owns a large number of fences that must be repaired annually. He trave ...
- VS2012及以上版本 程序打包部署详解
引用: http://blog.csdn.net/zhang_xinxiu/article/details/9099757 程序编写测试完成后接下来我们要做的是打包部署程序,但VS2012让人心痛的 ...
- RedHat9.0下载地址
RedHat下载:http://archive.download.redhat.com/pub/redhat/linux/9/en/iso/i386/
- Excel的 OleDb 连接串的格式(Provider=Microsoft.ACE.OLEDB)
string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;data source=" + filePath + ";Exten ...