博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Island Perimeter
阅读量:4986 次
发布时间:2019-06-12

本文共 1315 字,大约阅读时间需要 4 分钟。

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: 16Explanation: The perimeter is the 16 yellow stripes in the image below:

 

1 public class Solution { 2     public int islandPerimeter(int[][] grid) { 3         int islands = 0, neighbors = 0; 4         for (int i = 0; i < grid.length; i++) { 5             for (int j = 0; j < grid[i].length; j++) { 6                 if (grid[i][j] == 1) { 7                     islands++; 8                  9                     if (i < grid.length - 1 && grid[i + 1][j] == 1) neighbors++;10                     if (j < grid[i].length - 1 && grid[i][j + 1] == 1) neighbors++;11                 }12             }13         }14         return islands * 4 - neighbors * 2;15     }16 }

 

转载于:https://www.cnblogs.com/amazingzoe/p/6391357.html

你可能感兴趣的文章
四种百度文库资源直接下载的方法!不用代码,不用券!一键搞定!
查看>>
数据库-包和包体
查看>>
软件的知识产权保护
查看>>
7.20-7.24
查看>>
Bower前端包管理器
查看>>
Python练习题 047:Project Euler 020:阶乘结果各数字之和
查看>>
Docker私有仓库Harbor部署与使用
查看>>
2017年4月26日
查看>>
(第十周)Beta-2阶段成员贡献分
查看>>
希尔排序与快速排序
查看>>
洛谷p1966 火柴排队 (逆序对变形,目标排序
查看>>
AutoCAD的一些优化设置
查看>>
JSP中include的两种方法
查看>>
Ubuntu使用总结
查看>>
2019春第六周编程总结
查看>>
FLASH组件在FLEX中使用
查看>>
C++指针的指针和指针的引用
查看>>
android教程之intent对象
查看>>
python 学习笔记十五 django基础
查看>>
Python爬虫入门三之Urllib库的基本使用
查看>>