463. Island Perimeter Add to List
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.
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:

//
两个循环上下左右都检查一遍
第一次思考的时候想的是只检查右下,但是发现会重复,
然后又加了左上的判断。
后来仔细想想只对左上判断也是可以的。
class Solution {
public:
int islandPerimeter(vector<vector<int>>& grid) {
int sum=;
for(int i=;i<grid.size();i++)
for(int j=;j<grid[].size();j++){
if(grid[i][j]==){
sum+=;
grid[i][j]=;
if(i->=&&grid[i-][j]==)
sum--;
if(j->=&&grid[i][j-]==)
sum--;
if((i+<grid.size())&&grid[i+][j]==)
sum--;
if(j+<grid[].size()&&grid[i][j+]==)
sum--;
}
}
return sum;
}
};
463. Island Perimeter Add to List的更多相关文章
- 463. Island Perimeter - LeetCode
Question 463. Island Perimeter Solution 题目大意:给出一个二维数组1表示陆地0表示海,求陆地的周长 思路: 重新构造一张地图grid2即一个二维数组,比原数组大 ...
- [LeetCode&Python] Problem 463. 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】463. Island Perimeter
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- LeetCode 463 Island Perimeter 解题报告
题目要求 You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 rep ...
- 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 ...
- LeetCode - 463. Island Perimeter - O(MN)- (C++) - 解题报告
原题 原题链接 You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 ...
- 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(easy)
题目: You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 repr ...
随机推荐
- rabbitmq 命令行工具 执行失败.
修改cookie成一样 资料: http://zhiku8.com/rabbitmq-authentication-failed-rejected-by-the-remote- ...
- FTP主动连接与被动连接
FTP(File Transfer Protocol, FTP)是TCP/IP网络上两台计算机传送文件的协议,应用层的协议,它基于传输层, FTP是一个8位的客户端-服务器协议,能操作任何类型的文件而 ...
- JAVA中的Token 基于Token的身份验证
最近在做项目开始,涉及到服务器与安卓之间的接口开发,在此开发过程中发现了安卓与一般浏览器不同,安卓在每次发送请求的时候并不会带上上一次请求的SessionId,导致服务器每次接收安卓发送的请求访问时都 ...
- HTML5(。。。。不完整)
<!DOCTYPE html> 不区分大小写 <header>.<nav>.<article>.<section>.<sidebar ...
- 20145231《Java程序设计》第五次实验报告
实验五 Java网络编程及安全 实验内容 1.掌握Socket程序的编写: 2.掌握密码技术的使用: 3.设计安全传输系统. 实验要求 基于Java Socket实现安全传输 基于TCP实现客户端和服 ...
- matlab第一个小应用
今天安装了matlab,以前还是上线性代数的时候,老师让用过,以及水了一次数模的时候玩了一下.以前太年轻,总觉得这个用处不大虽然别人一直强调这个神器... 到了自己要用的时候才会意识到.大家可能也都听 ...
- RedisDesktopManager连接不上redis的解决方法
RedisDesktopManager是一款连接redis数据库的客户端. 背景:我是在自己机器上装的redis,使用的是虚拟机,系统是linux 版本是centeros-6.7 在使用这个连接red ...
- Springmvc跳转路径
forward转发地址栏不发生变化,redirect跳转地址栏变化,forward能把request域中的参数带给下一个,而redirect不会带过去,但是Springmvc的model虽然是基于re ...
- Apollo原理
https://github.com/ctripcorp/apollo/wiki/Apollo%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83%E8%AE%BE%E8%AE%A ...
- MapReduce-读取文件写入HBase
MapReduce直接写入HBase 代码如下 package com.hbase.mapreduce; import java.io.IOException; import org.apache.c ...