【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\ ...
- sw代码问题
1.环境版本问题,myeclipse 配 jdk1.7 不能用jdk1.8 :jdk1.6满足不了代码本身 报错为:The type java.io.ObjectInputStream cannot ...
- configure, make, make install都做了什么
1. 我的理解./configure: 确保接下来的make以及make install所依赖的文件没有问题make: build编译连接生成可执行程序make install: 将编译好的可执行 ...
- macos上安装wget命令
首先从Apple Store下载Xcode,然后安装Xcode,接着安装Homebrew包管理,类似于Ubuntu下的apt-get: 终端下输入ruby -e "$(curl -fsSL ...
- zabbix 布署实践【5 使用邮箱SMTP SSL推送告警邮件】
由于传统的邮件推送脚本使用smtp 25端口,在各大邮箱提供商已不适用,已经向SSL过渡,这里以QQ邮箱为例,使用SSL 465端口 登录zabbix-server 进入 cd /usr/lib/za ...
- Python 实现类似PHP的strip_tags函数功能,并且可以自定义设置保留标签
最近在研究 Python ,发现用的还是很不习惯,很多PHP里面很简单的功能在Python 里面都得找半天,而且很多功能都得自己实现. 今天做个采集,需要过滤内容中的标签,搞了一下午,貌似终于搞出来了 ...
- 【转】Zookeeper-Watcher机制与异步调用原理
声明:本文转载自http://shift-alt-ctrl.iteye.com/blog/1847320,转载请务必声明. Watcher机制:目的是为ZK客户端操作提供一种类似于异步获得数据的操作. ...
- CCF-出现次数最多的数
试题名称: 出现次数最多的数 试题编号:201312-1 时间限制: 1.0s 内存限制: 256.0MB 问题描述 给定n个正整数,找出它们中出现次数最多的数.如果这样的数有多个,请输出其中最小的一 ...
- centos7 安装openvswitch
安装 1.安装依赖包:yum -y install openssl-devel wget kernel-devel 2.安装开发工具:yum groupinstall "Developmen ...
- webuploader限制只上传图片文件
// 如果上传的文件不为图片格式, 那么就提示"上传文件格式不正确" if (file.type!="image/jpg" && file.ty ...