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. ThinkPHP支持的4种路由模式

    下面这个说法和示例,比较具有总结性.

  2. 《逆袭团队》第九次团队作业【Beta】Scrum meeting 3

    项目 内容 软件工程 任课教师博客主页链接 作业链接地址 团队作业9:Beta冲刺与团队项目验收 团队名称 逆袭团队 具体目标 (1)掌握软件黑盒测试技术:(2)学会编制软件项目总结PPT.项目验收报 ...

  3. LINUX部署JAVA项目

    Tomcat 应用服务器搭建好 安装 tomcat 所需依赖或工具软件 sudo yum -y update sudo yum -y install wget java unzip 使用 wget 下 ...

  4. jedis jedispool Redistemplate

    整理了之前学习 redis 的笔记,强烈建议看最后总结. 在大型系统数据读请求中,基本上90%都可以通过分布式缓存集群来抗下来,而 Redis 又是分布式缓存集群的主要践行者,因此了解 Redis 是 ...

  5. 学习Spring-Data-Jpa(十二)---投影Projections-对查询结果的扩展

    Spring-Data数据查询方法的返回通常的是Repository管理的聚合根的一个或多个实例.但是,有时候我们只需要返回某些特定的属性,不需要全部返回,或者只返回一些复合型的字段.Spring-D ...

  6. 2019-2020-1 20199302《Linux内核原理与分析》第九周作业

    一.进程调度的时机 硬中断和软中断 中断:程序执行过程中的强制性转移到操作系统内核相应的处理程序,起到切出指令流的作用. 中断处理程序:与进程无关的内核指令流. 进程切换:切换两个进程的内核堆栈. 硬 ...

  7. Oracle iops测试

    DECLARE  lat  INTEGER;  iops INTEGER;  mbps INTEGER;BEGIN  DBMS_RESOURCE_MANAGER.CALIBRATE_IO(4, 10, ...

  8. WinDbg常用命令系列---.load, .loadby (Load Extension DLL)

    .load, .loadby (Load Extension DLL) 简介 .load和.loadby命令将新的扩展DLL加载到调试器中. 使用形式 .load DLLName !DLLName.l ...

  9. redis 设置为只读模式

    数据库的只读模式,对于在系统出现重大故障,但是又不影响用户的查询操作还是很重要的 对于redis 设置只读模式需要分不同的场景 master-slave cluster single master-s ...

  10. s3git 使用git 管理云存储

    使用s3git 我们可以方便的基于git协议进行s3存储数据的版本管理,同时也提供了一个方便的golang 包, 我们可以集成到我们的应用中,但是有一点,目前已经没有再更新过了,但是设计理论很不错,实 ...