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

  1. 463. Island Perimeter - LeetCode

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

  2. [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 ...

  3. 463. Island Perimeter

    https://leetcode.com/problems/island-perimeter/ 在一个N×N的矩阵中,N<100,1代表岛,0代表海,岛内没有海,求岛的周长 [[0,1,0,0] ...

  4. 【LeetCode】463. Island Perimeter

    You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...

  5. LeetCode 463 Island Perimeter 解题报告

    题目要求 You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 rep ...

  6. 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 ...

  7. 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 ...

  8. 463. Island Perimeter岛屿周长

    [抄题]: You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 re ...

  9. 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 ...

随机推荐

  1. text_field text_tag 用法

    = f.text_field :tax_category_id, :value => @invoice.tax_category.name, :class => "form-co ...

  2. Java技术路线

    1.计算机基础: 1.1数据机构基础: 主要学习: 1.向量,链表,栈,队列和堆,词典.熟悉 2.树,二叉搜索树.熟悉 3.图,有向图,无向图,基本概念 4.二叉搜索A,B,C类熟练,9大排序熟悉. ...

  3. sed实例

    删除:d命令 $ sed '2d' example-----删除example文件的第二行. $ sed '2,$d' example-----删除example文件的第二行到末尾所有行. $ sed ...

  4. 2020年将热门的8大IT职业领域

    近日,外媒梳理了未来5年内,也是就是2020年仍将受到热捧的八大科技领域,为IT从业者如何做好长远规划.有针对性地培养自身技能.又不偏离热门岗位提供了参考.(图片来自网易) 2020年将热门的8大IT ...

  5. HashMap,LinkedHashMap和TreeMap的区别

    Map主要用于存储健值对,根据键得到值,因此不允许键重复(重复会覆盖),但允许值重复. 1. HashMap Hashmap是一个最常用的Map,它根据键的HashCode值存储数据,根据键可以直接获 ...

  6. Kubernetes Headless Service

    1. Headless Service headless service 需要将 spec.clusterIP 设置成 None. 因为没有ClusterIP,kube-proxy 并不处理此类服务, ...

  7. HDU 2603 二分匹配

    #include <queue>#include <vector>#include <cstdio>#include <cstring>#include ...

  8. 【算法】fhqtreap初探

    NOIP回来就一直想着学平衡树...平衡树写久了调不出来真的会头脑发热.jpg 大概只写了几道题... fhqtreap是不需要旋转的平衡树,仅使用分裂合并,一样可以保持平衡树的性质,并且可以非常简单 ...

  9. linux基础(1)-yum源配置

    用linux将近一年了,开始学的东西因为没经常用都忘记了,现在将笔记的东西慢慢整理成自己的博客,也算是看着自己进步.有些东西从他人博客笔记学的,有些是从视频学的,有些是自己填坑积累的. 在linux下 ...

  10. Apache Phoenix基本操作-2

    1. 如何映射一个Phoenix的表到一个Hbase的表? 你可以通过Create table/create view DDL语句在一个已经存在的hbase表上创建一个Phoenix表或者视图.对于C ...