463. Island Perimeter岛屿周长
[抄题]:
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:
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
+--+ +--+ +--+--+
| | + | | -> | |
+--+ +--+ +--+--+
4 + 4 - ? = 6 -> ? = 2
[一刷]:
- “连续统计”的题往往有准入门槛。必须第一个数符合条件,才能做后续的统计。
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
连续统计,对以第一个数有准入条件
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
733. Flood Fill 就是DFS把
[代码风格] :
class Solution {
public int islandPerimeter(int[][] grid) {
//cc
if (grid == null || grid.length == 0 || grid[0].length == 0) {
return 0;
} //ini
int island = 0, neighbor = 0; //for loop
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
//basic access
if (grid[i][j] == 1) {
island++;
if (i < grid.length - 1 && grid[i + 1][j] == 1) neighbor++;
if (j < grid[0].length - 1 && grid[i][j + 1] == 1) neighbor++;
}
}
} //return res;
return island * 4 - neighbor * 2;
}
}
463. 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] 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 - LeetCode
Question 463. Island Perimeter Solution 题目大意:给出一个二维数组1表示陆地0表示海,求陆地的周长 思路: 重新构造一张地图grid2即一个二维数组,比原数组大 ...
- [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 represen ...
- Leetcode463.Island Perimeter岛屿的周长
给定一个包含 0 和 1 的二维网格地图,其中 1 表示陆地 0 表示水域. 网格中的格子水平和垂直方向相连(对角线方向不相连).整个网格被水完全包围,但其中恰好有一个岛屿(或者说,一个或多个表示陆地 ...
- LeetCode 463 Island Perimeter 解题报告
题目要求 You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 rep ...
- 463. Island Perimeter
https://leetcode.com/problems/island-perimeter/ 在一个N×N的矩阵中,N<100,1代表岛,0代表海,岛内没有海,求岛的周长 [[0,1,0,0] ...
随机推荐
- L119
A big collaboration is trying to understand diseases of the psycheDiseases of the psyche have always ...
- k-means算法的优缺点以及改进
大家接触的第一个聚类方法,十有八九都是K-means聚类啦.该算法十分容易理解,也很容易实现.其实几乎所有的机器学习和数据挖掘算法都有其优点和缺点.那么K-means的缺点是什么呢? 总结为下: (1 ...
- python 编码 —— codecs 库
1. 对文件读写 import codecs fout = codecs.open('test.html', 'w', encoding='UTF-8') fout.write('<html&g ...
- mysqldumpslow使用说明。
mysql慢查询日志分析工具mysqldumpslow. 常用的方法: mysqldumpslow -s c -t 10 /var/run/mysqld/mysqld-slow.log # 取出使用最 ...
- UVA - 1608 Non-boring sequences (分治,中途相遇法)
如果一个序列中是否存在一段连续子序列中的每个元素在该子序列中都出现了至少两次,那么这个序列是无聊的,反正则不无聊.给你一个长度为n(n<=200000)的序列,判断这个序列是否无聊. 稀里糊涂A ...
- 洛谷 P2822 组合数问题
题目描述 组合数C_n^mCnm表示的是从n个物品中选出m个物品的方案数.举个例子,从(1,2,3) 三个物品中选择两个物品可以有(1,2),(1,3),(2,3)这三种选择方法.根据组合数的 ...
- nodejs 安装 cnpm 命令
npm install -g cnpm --registry=https://registry.npm.taobao.org
- JvisualVm添加远程监控
一.Weblogic远程监控 1.首先需要在远程的weblogic的域下面,找到/bin/ setDomainEnv.sh ,需要在此文件下加入如下内容: -Dcom.sun.management.j ...
- BZOJ3052:[WC2013]糖果公园
浅谈莫队:https://www.cnblogs.com/AKMer/p/10374756.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php?i ...
- 一些js和JQuery的方法
取值填值: js: get-- innerText, innerHTML, value; set-- innerText=?, innerHTML=?, value=?; JQuery: get-- ...