463. Island Perimeter

Easy

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

  1. Leetcode-463 Island Perimeter

    #463. Island Perimeter You are given a map in form of a two-dimensional integer grid where 1 represe ...

  2. 【LeetCode】Island Perimeter 解题报告

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

  3. 463. Island Perimeter - LeetCode

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

  4. [LeetCode] Island Perimeter 岛屿周长

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

  5. 463. Island Perimeter

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

  6. LeetCode Island Perimeter

    原题链接在这里:https://leetcode.com/problems/island-perimeter/ 题目: You are given a map in form of a two-dim ...

  7. 【LeetCode】463. Island Perimeter

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

  8. Island Perimeter

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

  9. leetcode算法:Island Perimeter

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

随机推荐

  1. P4281 [AHOI2008]紧急集合 / 聚会[LCA]

    解析 蒟蒻用的办法比较蠢,不如上面的各位大佬,直接化成一个式子了,我还是分类讨论做的. 下面正文. 猜想:最优集合点一定是三点任意两对点对应的路径的交点. 不妨这样想,如果任意两个人经过同一条路径,那 ...

  2. python基础知识笔记-集合

    集合使用花括号将元素之间用逗号隔开即可.集合的输出里不会出现相同的元素.集合中有并集.交集.差集等,以下为一些简单的表达: def main(): list1=[1,1,2,2,3,3] print( ...

  3. python应用-输出验证码

    from random import randint def generate_code (code_len): """ 生成确定位数的验证码 :param code_l ...

  4. 22、pandas表格、文件和数据库模块

    pandas官方文档:https://pandas.pydata.org/pandas-docs/stable/?v=20190307135750 pandas基于Numpy,可以看成是处理文本或者表 ...

  5. JSP学习之路

    1.JSP默认Encoding编码格式设置:UTF-8

  6. 27.SpringBoot和SpringMVC的区别

    所以,用最简练的语言概括就是: Spring 是一个“引擎”: Spring MVC 是基于Spring的一个 MVC 框架: Spring Boot 是基于Spring4的条件注册的一套快速开发整合 ...

  7. router.beforeEach、路由元信息、导航守卫与函数式编程

    一.函数的识别: 1.router.beforeEach:主函数.高阶函数.入口函数: 2.匿名参量函数:处理跳转过程中的附加逻辑 (to, from, next) => { if (to.ma ...

  8. springboot 打包插件spring-boot-maven-plugin打包机制及内部结构分析

    今日思语:I miss you? 何解? 我错过你了?我想你了? 当下许多公司都会选择使用springboot作为服务应用开发框架,springboot框架提供了一套自己的打包机制,是通过spring ...

  9. webapi HttpGet标签

    该标签可以指定路由如HttpGet["Test"],以前用的很顺,后来加了Area后,按照area/controller/Test的路径去访问报404,原因是HTTPGet指定路由 ...

  10. iOS 逆向工程(工具介绍)- 学习整理(转)

    一.class-dump 简介:顾名思义,就是用来导出目标对象的class信息的工具,私有方法声明也能导出来. 原理:利用 Objective-C语言的 runtime 特性,将存 在Mach-O 文 ...