LeetCode_463. Island Perimeter
463. Island Perimeter
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:
Input:
[[0,1,0,0],
[1,1,1,0],
[0,1,0,0],
[1,1,0,0]] Output: 16 Explanation: The perimeter is the 16 yellow stripes in the image below:
package leetcode.easy; public class IslandPerimeter {
public int islandPerimeter(int[][] grid) {
if (grid == null || grid.length == 0) {
return 0;
}
int result = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
if (grid[i][j] == 1) {
result += 4;
if (i > 0 && grid[i - 1][j] == 1) {
result -= 2;
}
if (j > 0 && grid[i][j - 1] == 1) {
result -= 2;
}
}
}
}
return result;
} @org.junit.Test
public void test() {
int[][] grid = { { 0, 1, 0, 0 }, { 1, 1, 1, 0 }, { 0, 1, 0, 0 }, { 1, 1, 0, 0 } };
System.out.println(islandPerimeter(grid));
}
}
LeetCode_463. Island Perimeter的更多相关文章
- Leetcode-463 Island Perimeter
#463. Island Perimeter You are given a map in form of a two-dimensional integer grid where 1 represe ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 463. Island Perimeter - LeetCode
Question 463. Island Perimeter Solution 题目大意:给出一个二维数组1表示陆地0表示海,求陆地的周长 思路: 重新构造一张地图grid2即一个二维数组,比原数组大 ...
- [LeetCode] 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 Island Perimeter
原题链接在这里:https://leetcode.com/problems/island-perimeter/ 题目: You are given a map in form of a two-dim ...
- 【LeetCode】463. Island Perimeter
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- Island Perimeter
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- leetcode算法:Island Perimeter
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
随机推荐
- IoT设备上的恶意软件——通过漏洞、弱密码渗透
2018年,是 IoT 高速发展的一年,从空调到电灯,从打印机到智能电视,从路由器到监控摄像头统统都开始上网.随着5G网络的发展,我们身边的 IoT 设备会越来越多.与此同时,IoT 的安全问题也慢慢 ...
- Word 页码设置教程:如何删除封面和目录的目录?
我们常写的报告大都由封面.目录.正文和附录组成,但是页码通常是从正文开始的,所以下面介绍如何从指定页面开始设置页码. 在介绍之前需要了解一下分隔符的作用.分隔符大体分成分页符和分节符. 分页符细分的几 ...
- 那周余嘉熊掌将得队对男上加男,强人所男、修!咻咻! 团队的Beta产品测试报告
作业格式 课程名称:软件工程1916|W(福州大学) 作业要求:Beta阶段团队项目互评 团队名称: 那周余嘉熊掌将得队 作业目标:项目互测互评 队员学号 队员姓名 博客地址 备注 221600131 ...
- HDU6625: three arrays (字典树处理xor)
题意:给出A数组,B数组,你可以对A和B分别进行重排列,使得C[i]=A[i]^B[i]的字典序最小. 思路:对于这类题,显然需要建立字典树,然后某种形式取分治,或者贪心. 假设现在有了两颗字典树A ...
- 【IIS】跨域(转)
Access to XMLHttpRequest at 'http://*****/.dae' from origin 'http://192.168.198.21:22222' has been b ...
- 命令行创建react.js项目
npm install -g create-react-app /*搭建一个全局的脚手架*/ create-react-app my-demo /*创建项目 my-demo是项目名字* ...
- Ad-hoc--拉丁文--for this purpose only
Ad-hoc这个词来源于拉丁语,在百度上解释为“for this purpose only”, 在wiki上解释为“for this”,其中文在wiki上被解释成包含“特设的.特定目的的(地).即席的 ...
- Python 弹出框代码
from ctypes import * user32 = windll.LoadLibrary('user32.dll')#调用dll文件 #a是得到弹出框的选择按钮的值 user32.Mess ...
- zabbix_server [22842]: cannot open log: cannot create semaphore set: [28] No space left on device
zabbix server服务没启动,查看日志显示 zabbix_server [22842]: cannot open log: cannot create semaphore set: [28] ...
- CCF 201909-5 城市规划
试题编号: 201909-5 试题名称: 城市规划 时间限制: 3.0s 内存限制: 512.0MB 问题描述: 几乎是Gym102222G的原版,详解见上一篇博文 /* 贡献+树形dp+01背包 * ...