LeetCode之Island Perimeter

1、题目

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:


2、代码实现

public class Solution {public int islandPerimeter(int[][] grid) {if (grid == null)return 0;//gril.length就是行数 row,grid.length就是列数grilint row = grid.length;int col = grid[0].length;int sum = 0, count = 0;for (int i = 0; i < row; ++i) {for (int j = 0; j < col; ++j) {if (grid[i][j] == 1) {if (i - 1 == -1) {count++;} else {if (grid[i - 1][j] == 0) {count++;}}if (i + 1 == row) {count++;} else {if (grid[i + 1][j] == 0) {count++;}}if (j - 1 == -1) {count++;} else {if (grid[i][j - 1] == 0) {count++;}}if (j + 1 == col) {count++;} else {if (grid[i][j + 1] == 0) {count++;}}}}}return count;    }
}

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/291378.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java获取tomcat目录结构_tomcat目录结构简介_动力节点Java学院整理

tomcat目录结构简介如果我们有一个web应用&#xff0c;名称为“mail”(同时也是web应用所在目录的名称)&#xff0c;那么其目录内不同类型的文件应该服从如下放置的规则&#xff1a;一般来讲&#xff1a;对于html、jsp、css、js文件等&#xff0c;可以直接放置在web应用所在目录…

Linux和Windows下部署BeetleX服务网关

有朋友希望写一篇BeetleX服务网关部署到Linux和windows下并以服务的方式运行的介绍文章。接下详细介绍如何做并简单介绍一下网的使用。首先需要在官网(beetlex-io.com)下载对应版本的BeetleX服务网关&#xff08;现阶段只支持linux64和windows64&#xff09;&#xff0c;下载完…

HDU 1978 How many ways DP问题

How many ways Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2568 Accepted Submission(s): 1509 Problem Description这是一个简单的生存游戏&#xff0c;你控制一个机器人从一个棋盘的起始点(1,1)走到棋盘的…

课堂练习-找水王绪

题目&#xff1a;三人行设计了一个灌水论坛。信息学院的学生都喜欢在上面交流灌水&#xff0c;传说在论坛上有一个“水王”&#xff0c;他不但喜欢发帖&#xff0c;还会回复其他ID发的每个帖子。坊间风闻该“水王”发帖数目超过了帖子数目的一半。 如果你有一张当前论坛的…

LeetCode之Nim Game

1、题目 You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove …

适配器模式和装饰模式

1 什么是适配器模式 当我要使用一个类时&#xff0c;但是我发现它的接口不是我想要的模样&#xff0c;这个时候&#xff0c;我可以使用适配器模式&#xff0c;新设计一个类&#xff0c;然后这个类提供我想要的接口&#xff0c;在它里面引用原来的类。 2 什么是装饰模式 当我想要…

java添加事件监听器_Java事件监听器的四种实现方式

自身类作为事件监听器外部类作为事件监听器匿名内部类作为事件监听器内部类作为事件监听器自身类作为事件监听器:1 import javax.swing.*;2 import java.awt.*;3 import java.awt.event.*;45 /**6 *Java事件处理机制:自身类作为事件监听器7 *authorWinty(wintysgmail.com)8 *ve…

使用Brighter实现轻量型独立管道

前言上次&#xff0c;我们介绍了使用MediatR的Behaviors功能&#xff0c;在业务层实现管道模式。(《为什么应该在业务层实现管道模式&#xff0c;而不用ASP.NET Core Middleware实现 | 2点原因和实现方式》)但是&#xff0c;这种管道有个特点或者说缺点&#xff0c;不管你需不需…

Adobe Air 写文件如何换行

在用Air打log的时候发现,在字符串后面加"\n"并不能实现换行.百度一下才知道windows的换行是"\r\n".Mac OS 和 Linux换行符是"\n". 不同操作系统的换行符可能不一样.File有个静态属性File.lineEnding.用这个就可以了 下面是这个属性的官方帮助文档…

CentOS6最小化安装默认启动的服务说明

centos6.2最小化安装后执行chkconfig --list,显示所有服务&#xff0c;如下图&#xff1a;下边分别进行说明&#xff1a;auditd&#xff1a;审核守护进程当 auditd 运行的时候&#xff0c;审核信息会被发送到一个用户配置日志文件中&#xff08;默认的文件是 /var/log/audit/au…

网页中插入javascript的几种方法

网页中插入javascript的方法常见的有两种&#xff1a; 一、直接使用html标记 JavaScript 可以出现在 html的任意地方。使用标记<script>…</script>&#xff0c;你可以在 HTML 文档的任意地方插入 JavaScript&#xff0c;甚至在<HTML>之前插入也不成问题。不…

LeetCode之Max Consecutive Ones

1、题目 Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are consecutive 1s.The maximum number of consecutive 1s is 3.Note: T…

java exception 行号_java日志记录错误的文件_方法_行号_报错信息

1、java日志记录错误的文件、方法、行号、报错信息StackTraceElement s e.getStackTrace()[0];1.1、记录保存的文件s.getFileName()1.2、记录保存的方法s.getMethodName()1.3、记录报错的行号 s.getLineNumber()1.4、记录报错的信息(不全面) e.getMessage()1.5、互利报错的类名…

[有奖励]GeneralUpdate开源项目招募开发者

[有奖励]GeneralUpdate开源项目招募开发者希望看到这篇文章的小伙伴&#xff0c;能看完这篇文章顺便帮忙给项目点一下“star”、转发、“在看”。先在这里谢谢各位了。github仓库地址&#xff1a;https://github.com/WELL-E/AutoUpdatergitee仓库地址&#xff1a;https://gitee…

C/C++ 读取16进制文件

1.为什么有这种需求 因为有些情况需要避免出现乱码。不管什么编码都是二进制的&#xff0c;这样表示为16进制就可以啦。 2.如何读取16进制文件 最近编程用这一问题&#xff0c;网上查了一下&#xff0c;感觉还是自己写吧。 16进制数据一般是:text0x340xb5...&#xff0c;就是0x…

【转】shell pipe与输入输出重定向的区别

http://www.cnblogs.com/chengmo/archive/2010/10/21/1856577.html转载于:https://www.cnblogs.com/qrlozte/p/4465120.html

LeetCode之Remove Duplicates from Sorted Array

1、题目 Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example, Given input array nu…

Upgrade Oracle GI 11.2.0.4_to_12.1.0.2

12.1.0.2已经发布一段时间了&#xff0c;随着用户的增多&#xff0c;目前12C的版本稳定性&#xff0c;以及各个方面的功能性&#xff0c;得到大家的认可。很多用户&#xff0c;需要将数据库从低版本升级到12C&#xff0c;本文就升级过程&#xff0c;做了详细的记录。&#xff0…

基于PaddleOCR实现AI发票识别的Asp.net Core应用

简要介绍用户批量上传需要识别的照片,上传成功后,系统会启动Hangfire后台Job开始调用PaddleOCR服务返回结果,这个过程有点类似微服务的架构模型。PaddleOCRPaddleOCR是百度AI团队开源的一个项目&#xff0c;应该是目前所有免费开源OCR项目中识别效果最好的,具体可以通过PaddleO…

java socket 路由_JAVA简单的Socket网络编程!CS

好久没写socket代码了&#xff0c;也忘了八九成了……话不多说&#xff01;直接上代码吧&#xff01;Server服务package test1;import java.io.*;import java.net.*;public class MyServer {public static void main(String[] args) throws IOException{ServerSocket server n…