Leetcode463.Island Perimeter岛屿的周长
给定一个包含 0 和 1 的二维网格地图,其中 1 表示陆地 0 表示水域。
网格中的格子水平和垂直方向相连(对角线方向不相连)。整个网格被水完全包围,但其中恰好有一个岛屿(或者说,一个或多个表示陆地的格子相连组成的岛屿)。
岛屿中没有“湖”(“湖” 指水域在岛屿内部且不和岛屿周围的水相连)。格子是边长为 1 的正方形。网格为长方形,且宽度和高度均不超过 100 。计算这个岛屿的周长。
示例 :
输入: [[0,1,0,0], [1,1,1,0], [0,1,0,0], [1,1,0,0]] 输出: 16
class Solution {
public:
int islandPerimeter(vector<vector<int>>& grid) {
int r = grid.size();
if(r == 0)
return 0;
int c = grid[0].size();
int cnt = 0;
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
if(grid[i][j] == 0)
continue;
//上下左右
if(i - 1 < 0 || grid[i - 1][j] == 0)
cnt++;
if(i + 1 >= r || grid[i + 1][j] == 0)
cnt++;
if(j - 1 < 0 || grid[i][j - 1] == 0)
cnt++;
if(j + 1 >= c || grid[i][j + 1] == 0)
cnt++;
}
}
return cnt;
}
};
Leetcode463.Island Perimeter岛屿的周长的更多相关文章
- 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 ...
- 463 Island Perimeter 岛屿的周长
详见:https://leetcode.com/problems/island-perimeter/description/ C++: class Solution { public: int isl ...
- Leetcode-463 Island Perimeter
#463. Island Perimeter You are given a map in form of a two-dimensional integer grid where 1 represe ...
- Leetcode463. Island Perimeter
题目 给定一个包含 0 和 1 的二维网格地图,其中 1 表示陆地 0 表示水域. 网格中的格子水平和垂直方向相连(对角线方向不相连).整个网格被水完全包围,但其中恰好有一个岛屿(或者说,一个或多个表 ...
- [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岛屿周长
[抄题]: You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 re ...
- [LeetCode] 463. Island Perimeter 岛的周长
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- [Swift]LeetCode463. 岛屿的周长 | Island Perimeter
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- C#LeetCode刷题之#463-岛屿的周长(Island Perimeter)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3794 访问. 给定一个包含 0 和 1 的二维网格地图,其中 1 ...
随机推荐
- <每日一题>题目12:列表解析及zip、dict函数的简单应用
''' 分析: 1.列表解析:迭代机制的一种应用 语法: [expression for iter_val in iterable] [expression for iter_val in itera ...
- 2019-5-21-Total-Commander-显示文件包含文件名扩展
title author date CreateTime categories Total Commander 显示文件包含文件名扩展 lindexi 2019-5-21 11:37:6 +0800 ...
- thinkphp 子查询
从3.0版本开始新增了子查询支持,有两种使用方式: 大理石平台检验标准 1.使用select方法 当select方法的参数为false的时候,表示不进行查询只是返回构建SQL,例如: // 首先构造子 ...
- 报javax.servlet.ServletException: Servlet.init() for servlet [springmvc] threw exception的解决记录
1.异常详情: 2.异常分析: 从异常的详情中看出:companyService未找到,出现这种情况的愿意可能是companyServiceImpl类没有交给IOC容器管理,但是经过我已经在该类上打了 ...
- 莫烦PyTorch学习笔记(五)——模型的存取
import torch from torch.autograd import Variable import matplotlib.pyplot as plt torch.manual_seed() ...
- 利用IDEA构建springboot应用--controller例子
微服务 微服务是一个新兴的软件架构,就是把一个大型的单个应用程序和服务拆分为数十个的支持微服务.一个微服务的策略可以让工作变得更为简便,它可扩展单个组件而不是整个的应用程序堆栈,从而满足服务等级协议. ...
- 从xmlns的作用说起
查了资料和自己实践后,得出了一些关于xml和xmlns的结论 看一个最常见的javaweb 中xml配置文件的开头: <?xml version="1.0" encoding ...
- re 模块 (正则的使用)
一.正则表达式 英文全称: Regular Expression. 简称 regex或者re.正则表达式是对字符串操作的一种逻辑公式. 我们一般使用正则表达式对字符串进行匹配和过滤. 使用正则的优缺点 ...
- JS写一个方法,传入一个数组,返回该数组的层深(维度)
现在我们有一个多维数组,我们想得到该数组的层深,即最大维度 如:var arr = [1, [4,[5,6,[7]]], [2,3]] = 0:返回4:那么我们该怎么做呢? 核心思想:递归,循环遍历 ...
- Linux 启动dubbo管控台: