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的更多相关文章

  1. 【LeetCode】463. Island Perimeter 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 减去相交部分 参考资料 日期 题目地址:https: ...

  2. 【leetcode】 463. Island Perimeter

    题目: 以二维数组形式表示坐标岛屿,求边长. 例子: [[0,1,0,0], [1,1,1,0], [0,1,0,0], [1,1,0,0]] Answer: 16 Explanation: The ...

  3. 【LeetCode】976. Largest Perimeter Triangle 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...

  4. 【leetcode】976. Largest Perimeter Triangle

    题目如下: Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero ...

  5. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  6. 463. Island Perimeter - LeetCode

    Question 463. Island Perimeter Solution 题目大意:给出一个二维数组1表示陆地0表示海,求陆地的周长 思路: 重新构造一张地图grid2即一个二维数组,比原数组大 ...

  7. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  8. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  9. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

随机推荐

  1. Kattis - Peragrams

    Peragrams Photo by Ross Beresford Per recently learned about palindromes. Now he wants to tell us ab ...

  2. 【6】锋利的 jQuery 笔记

    1. 代码技巧 1. 利用 id, class 实现同级隐藏显示 效果如下: 2. 字体放大效果 效果图: 3. tab 切换 效果图: 4. 切换样式 添加 Cookie 效果图: 5. 编写插件 ...

  3. ffmpeg的安装--opencv视频处理必备

    安装yasm(ffmpeg必备)wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gztar xzvf yasm-1. ...

  4. memcache学习和分析

    Memcached 特点• 具有非常快的处理速度• 缺乏认证以及安全管制,应将其放置在防火墙之后• 重启后数据全部丢失• 可以给数据设置有效期• 适合使用大量低CPU的机器搭建集群• 各节点之间各自独 ...

  5. maven项目如何引用本地的jar包

    下载该jar包到本地(如下载目录结构为:D:\Users\lu.wang\Downloads\searchservice\searchservice\jar\ttd.search.searchserv ...

  6. python3 列表 函数

    python3中list的所有函数 list是有序的,元素个数无限的,元素类型多样的,可变的 增加 # 'append', 增加对象# 'insert', 指定位置增加# 'extend', 增加可迭 ...

  7. intellij idea 生成UUID

    Intellij IDEA 默认没启用这个功能 需要手动设置一下 , 下面是路径 Setting->Inspections->Serialization issues->Serial ...

  8. brew install nvm

    brew install nvm mkdir ~/.nvm nano ~/.bash_profilectrl+x 退出 source ~/.bash_profile echo $NVM_DIR nvm ...

  9. ajax客户端请求与服务端响应浅谈

    AJAX,即Asynchronous Javascript And XML,AJAX本质是在HTTP协议的基础上以异步的方式与服务器进行通信. 所谓的异步,是指某段程序执行不会阻塞其他程序执行,其表现 ...

  10. zabbix 布署实践【5 使用邮箱SMTP SSL推送告警邮件】

    由于传统的邮件推送脚本使用smtp 25端口,在各大邮箱提供商已不适用,已经向SSL过渡,这里以QQ邮箱为例,使用SSL 465端口 登录zabbix-server 进入 cd /usr/lib/za ...