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.

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:

给一个二维的格子图,格子是1表示陆地,0表示水,格子水平或者垂直连接,若干的格子连在一起形成一个小岛,只有一个相连的岛,且岛中没有湖,求岛的周长。

解法1: 遇到为1的格子,检验四个方向是否和陆地相连

解法2: 遇到为1的格子,检查上面和左面是否和陆地相连

Java:

public class Solution {
public int islandPerimeter(int[][] grid) {
int islands = 0, neighbours = 0; for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
if (grid[i][j] == 1) {
islands++; // count islands
if (i < grid.length - 1 && grid[i + 1][j] == 1) neighbours++; // count down neighbours
if (j < grid[i].length - 1 && grid[i][j + 1] == 1) neighbours++; // count right neighbours
}
}
} return islands * 4 - neighbours * 2;
}
}

Python:

def islandPerimeter(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
ans = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
ans += 4
if i > 0 and grid[i-1][j] == 1:
ans -= 2
if j > 0 and grid[i][j-1] == 1:
ans -= 2
return ans  

Python:

class Solution(object):
def islandPerimeter(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
if not grid:
return 0 def sum_adjacent(i, j):
adjacent = (i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1),
res = 0
for x, y in adjacent:
if x < 0 or y < 0 or x == len(grid) or y == len(grid[0]) or grid[x][y] == 0:
res += 1
return res count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
count += sum_adjacent(i, j)
return count  

Python:

class Solution:
def islandPerimeter(self, grid):
if not grid:
return 0 N = len(grid)
M = len(grid[0])
ans = 0
for i in range(N):
for j in range(M):
for k in [(-1, 0), (0, 1), (1, 0), (0, -1)]:
if i+k[0] < 0 or i + k[0] >= N or j+k[1] < 0 or j+k[1] >= M or grid[i+k[0]][j+k[1]] == 0:
ans += 1
return ans  

C++:

class Solution {
public:
int islandPerimeter(vector<vector<int>>& grid) {
if (grid.empty() || grid[0].empty()) return 0;
int m = grid.size(), n = grid[0].size(), res = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == 0) continue;
if (j == 0 || grid[i][j - 1] == 0) ++res;
if (i == 0 || grid[i - 1][j] == 0) ++res;
if (j == n - 1 || grid[i][j + 1] == 0) ++res;
if (i == m - 1 || grid[i + 1][j] == 0) ++res;
}
}
return res;
}
};

C++:

class Solution {
public:
int islandPerimeter(vector<vector<int>>& grid) {
if (grid.empty() || grid[0].empty()) return 0;
int res = 0, m = grid.size(), n = grid[0].size();
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == 0) continue;
res += 4;
if (i > 0 && grid[i - 1][j] == 1) res -= 2;
if (j > 0 && grid[i][j - 1] == 1) res -= 2;
}
}
return res;
}
};

  

Followup: 如果不止一个岛屿  

All LeetCode Questions List 题目汇总

[LeetCode] 463. Island Perimeter 岛的周长的更多相关文章

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

  2. LeetCode 463. Island Perimeter (岛的周长)

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

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

  4. LeetCode 463 Island Perimeter 解题报告

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

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

  6. 3. leetcode 463 Island Perimeter

    思路:设原始周长为4*节点数,每当出现一次相邻的情况,原始周长会减2.

  7. 463 Island Perimeter 岛屿的周长

    详见:https://leetcode.com/problems/island-perimeter/description/ C++: class Solution { public: int isl ...

  8. 463. Island Perimeter - LeetCode

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

  9. 【LeetCode】Island Perimeter 解题报告

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

随机推荐

  1. Codeforces Round #555 (Div. 3) F. Maximum Balanced Circle

    F. Maximum Balanced Circle 题目链接 题意 给出\(n\)个数,现在要从中选出最多的数\(b_i,b_{i+1},\cdots,b_k\),将这些数连成一个环,要求两两相邻的 ...

  2. python打造seo必备工具-自动查询排名

    因为工作需要,利用业余时间开发的,可以查询百度排名+360排名工具,附上代码. #360搜索排名查询 # -*- coding=utf-8 -*- import requests from lxml ...

  3. Laravel —— 多模块开发

    Laravel 框架比较庞大,更适用于比较大的项目. 为了整个项目文件结构清晰,不同部分分为不同模块很有必要. 一.安装扩展包 1.根据不同 Laravel 版本,选择扩展包版本. packagest ...

  4. Linux学习笔记——管道PIPE

    管道:当从一个进程连接数据流到另一个进程时,使用术语管道(pipe).# include <unistd.h> int pipe(int filedes[2]); //创建管道 pipe( ...

  5. Mybatis框架-resultMap元素的自动映射级别

    resultMap的自动映射级别:分为三种:NONE  PARTIAL  FULL 其中默认的属性是:PARTIAL:开启自动匹配,会自动匹配数据库中的字段名和实体类中的属性名,如果一致,就能匹配上, ...

  6. Greenplum 与 PostgreSQL 修改元数据(catalog)的方法 allow_system_table_mods

    背景 PostgreSQL大量的信息保存在元数据中,所有的元数据都是内部维护的,例如建表.建索引.删表等操作,自动维护元数据. 在某些迫不得已的情况下才可能需要直接对元数据进行修改. 默认情况下,用户 ...

  7. a list of frequently asked questions about Circus

    转自:https://circus.readthedocs.io/en/latest/faq/,可以帮助我们了解circus 的使用,以及问题解决 How does Circus stack comp ...

  8. gulp+webpack多页应用开发,webpack仅处理打包js

    项目背景:一个综合网站,开发模式为后端嵌套数据,前端开发静态页面和部分组件. 问题:gulp任务处理自动刷新.sass编译等都是极好的.但是对于js的处理并不是很好,尤其是项目需要开发组件时候,如评论 ...

  9. (4.1)打造简单OS-小实验[图形显示]

    主要是实现<简单打造OS>第四小节说到的一个图形界面的实验项目 1.mbr boot.inc ;------------- loader和kernel ---------- LOADER_ ...

  10. mapreduce数据处理——统计排序

    接上篇https://www.cnblogs.com/sengzhao666/p/11850849.html 2.数据处理: ·统计最受欢迎的视频/文章的Top10访问次数 (id) ·按照地市统计最 ...