463. Island Perimeter Add to List
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.
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 sum=;
for(int i=;i<grid.size();i++)
for(int j=;j<grid[].size();j++){
if(grid[i][j]==){
sum+=;
grid[i][j]=;
if(i->=&&grid[i-][j]==)
sum--;
if(j->=&&grid[i][j-]==)
sum--;
if((i+<grid.size())&&grid[i+][j]==)
sum--;
if(j+<grid[].size()&&grid[i][j+]==)
sum--;
}
}
return sum;
}
};
463. Island Perimeter Add to List的更多相关文章
- 463. Island Perimeter - LeetCode
Question 463. Island Perimeter Solution 题目大意:给出一个二维数组1表示陆地0表示海,求陆地的周长 思路: 重新构造一张地图grid2即一个二维数组,比原数组大 ...
- [LeetCode&Python] Problem 463. Island Perimeter
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- 463. Island Perimeter
https://leetcode.com/problems/island-perimeter/ 在一个N×N的矩阵中,N<100,1代表岛,0代表海,岛内没有海,求岛的周长 [[0,1,0,0] ...
- 【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 ...
- 463. Island Perimeter岛屿周长
[抄题]: You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 re ...
- LeetCode: 463 Island Perimeter(easy)
题目: You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 repr ...
随机推荐
- Linux基础系列:常用命令(3)
作业一: ) 将用户信息数据库文件和组信息数据库文件纵向合并为一个文件/.txt(覆盖) cat /etc/passwd /etc/group > /test/.txt ) 将用户信息数据库文件 ...
- LeetCode:搜索二维矩阵【74】
LeetCode:搜索二维矩阵[74] 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的 ...
- smarty变量调节器与函数
smarty自带了一些变量调节器与内置函数,都在libs/plugins目录下,变量调节器以modifier开头,函数以function开头,而且我们可以自定义变量调节器与函数,熟练运用之后会极大地提 ...
- flex datagrid 导出csv
public function exportToCSV(dataGrid:DataGrid):void { var dataProviderCollection:ArrayCollection = d ...
- Python自然语言处理-系列一
一:python基础,自然语言概念 from nltk.book import * 1,text1.concordance("monstrous") 用语索引 2,tex ...
- 【HackerRank】Pairs
题目链接:Pairs 完全就是Two Sum问题的变形!Two Sum问题是要求数组中和正好等于K的两个数,这个是求数组中两个数的差正好等于K的两个数.总结其实就是“骑驴找马”的问题:即当前遍历ar[ ...
- linux环境vim升级到vim74
作为编辑器之神,vim7.4已经发布近两个月了.从vim7.3到vim7.4,时隔两年之久,做了多项改正和性能提升,作为Linux Geeksters的你,怎能错过. 由于各大主流linux操作系统都 ...
- 关闭Selinux 命令
在nginx 配置文件中,新增location中的内容,完成后,web上403报错 方法:关闭Selinux即可. Follow below steps: 虚拟机服务器环境补充: # vim/etc/ ...
- JavaWeb Session
1. Session概述 1.1. 什么是Session Session一般译为会话,是解决Http协议的无状态问题的方案,可以将一次会话中的数据存储在服务器端的内存中,保证在下一次的会话中可以使用. ...
- JSP JavaBeans
Javabean的设计原则 公有类 无参公有构造方法 私有属性 getter和setter方法 在Jsp页面中如何使用Javabeans? 像使用普通Java类一样,创建JavaBeans实例. 在J ...