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 ... 
随机推荐
- MySQL数据库(8)_MySQL数据库总结
			sql语句规范 sql是Structured Query Language(结构化查询语言)的缩写.SQL是专为数据库而建立的操作命令集,是一种功能齐全的数据库语言. 在使用它时,只需要发出“做什么” ... 
- LeetCode:全排列【46】
			LeetCode:全排列[46] 题目描述 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2 ... 
- PAT 天梯赛 L1-009. N个数求和 【模拟】
			题目链接 https://www.patest.cn/contests/gplt/L1-009 思路 每一步每一步 往上加,但是要考虑 溢出,所以用 LONG LONG 而且 每一步 都要约分 才能保 ... 
- Architecture Patterns
			This chapter provides guidelines for using architecture patterns. Introduction Patterns for system a ... 
- vscode常用快捷键及常用设置
			快捷键 ctrl+b 切换侧边栏 ctrl+\ 拆分编辑器 ctrl+鼠标滚轮 缩放编辑器的字体 alt+shift+f 整理代码格式 alt+z 切换自动换行 ctrl+· 打开终端调试 ctrl+ ... 
- sublime text3自动同步左边栏颜色背景为编辑栏颜色
			下面的步骤需要安装Package Control插件,如果你已经安装,可跳过本步骤,直接看第二步. 第一步:安装Package Control插件: 按Ctrl+`调出console(注:安装有QQ输 ... 
- 【Head First Servlets and JSP】笔记13:session & cookie
			session的接口 杀死会话 cookie的性质 cookie的接口 再总结——cookie.session.JSESSIONID的前世今生 简单的定制cookie示例 1.session的接口,配 ... 
- 仿Facebook登录表单
			在线演示 本地下载 
- idea ource 1.5 中不支持 switch 中存在字符串
			报错内容如下: Error:(49, 20) java: -source 1.5 中不支持 switch 中存在字符串, (请使用 -source 7 或更高版本以允许 switch 中存在字符串) ... 
- JAVA学习前十天:小结、面向对象之”扑克牌“例子
			2016年4月26号正式步入JAVA学习课堂,学习了第一节JAVA课程,由于以前有C语言基础,所以课程有点快! 第一天:学习了教材的第一部分,第一部分总共两章,分别是JAVA简介和JAVA基础语法. ... 
