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 ...
随机推荐
- SpringMVC:学习笔记(7)——验证器(JSR303)
JSR 303(Bean Validation ) 说明: 在任何时候,当你要处理一个应用程序的业务逻辑,数据校验是你必须要考虑和面对的事情.应用程序必须通过某种手段来确保输入进来的数据从语义上来讲是 ...
- yii 2 局部关闭 CSRF 拦截
最近在拿 yii 2.0 开发ajax提交,在 post 请求接口时,提示数据无法验证,于是查询 yii 错误日志,发现错误为 exception ‘yiiwebBadRequestHttpExcep ...
- 【HackerRank】Utopian tree
The Utopian tree goes through 2 cycles of growth every year. The first growth cycle of the tree occu ...
- 简要总结ajax工作原理及优缺点
虽然在实际的项目中使用多种ajax请求,但就其工作原理,优缺点尚未深入总结, 参考:http://www.cnblogs.com/SanMaoSpace/archive/2013/06/15/3137 ...
- Eclipse常用插件安装_PropertiesEditor
properties文件在项目中多用做i18n国际化支持的配置文件,在properties文件中出现的中文信息都要转换为Unicode文本,一般的做法都是使用JDK自带的native2ascii工具进 ...
- IntelliJ IDEA 中 右键新建时,选项没有Java class的解决方法和具体解释
我是在别人问我的时候,才发现还可以有这个问题,主要是他新项目直接打开,什么都没配置,就打算新建文件,其实可以直接看编辑器右下角的event log,那个对话框点开的话,可以add as maven p ...
- EntityFramework 学习 一 Execute Native SQL Query
SQL query for entity types: using (var ctx = new SchoolDBEntities()) { var studentList = ctx.Student ...
- mssql 函数STUFF 的用法
DECLARE @limitDay INT;SET @limitDay = 92;IF DATEDIFF(DAY, '2017-12-13 00:00:00', '2017-12-13 18:00:0 ...
- 阅读linux内核代码的工具-- Source Insight
http://blog.csdn.net/luckyaslan/article/details/7869235 Step 1:安装Source Insight并启动程序 可以进入图1界面,在工具条上有 ...
- dubbox 学习
目录 编译源码 发布dubbo的jar包到私库 安装dubbo-admin 安装monitor Springboot+dubbox 其他 编译源码 dubbox是没有安装包的,所以我们只能先下载源码 ...