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] ...
随机推荐
- ionic2——学习指引-学习资源汇总
Ionic2 官网............................官网的文档非常好,超级全,一定要细心看中文文档.....................比较简单 Angular 2 官网.. ...
- uva10815(set的应用)
紫书例题,这道题的例程让我长了知识.以前没有用过cctype和stringstream相关的东西.很实用,值得学习. #include <cctype>的函数 c++中应该是#includ ...
- 使用Snapdragon Profiler工具分析
http://blog.csdn.net/cgx090902/article/details/73849202 Snapdragon Profiler(骁龙分析器)是一款性能分析软件,在Windows ...
- Raid信息丢失数据恢复及oracle数据库恢复验证方案
早些时候,有个客户14块盘的磁盘阵列出现故障,需要恢复的数据是oracle数据库,客户在寻求数据恢复技术支持,要求我提供详细的数据恢复方案,以下是提供给客户的详细数据恢复解决方案,本方案包含Raid数 ...
- 异常:java.lang.IllegalStateException: No instances found of configserver(里面是一个微服务名)
今天本地测试代码时出现了个异常,该异常出现的原因是:微服务启动的顺序出现了问题: 应该先启动本地eureka,然后在启动本地配置中心,然后在启动具体的微服务.
- think python chapter2
1.An assignment statement creates a new variable and gives it a value: Variable names can be as long ...
- Azure基于角色的用户接入控制(RBAC)
RBAC是Role Based Access Control是基于角色的接入控制的简称.在Azure推出ARM以后,对Azure各种资源的管理粒度已经非常细致,使得RBAC成为可能. 通过RBAC可以 ...
- eclipse -- propedit 安装.
SSH 项目中.用到国际化文件,Message.properties 但里面的文本都是 unicode 格式的,用起来不谁.而且也不想通过其它工具转码来使用. 可以通过安装 eclipse 的 pro ...
- Spring 与 MyBatis 的整合
本文讨论 Spring 与 MyBatis 的整合. 在 beans.xml 中我们定义了两个 bean: SqlSessionFactoryBean.SqlSessionTemplate. 1.Sq ...
- java代码。从来没想过java里的继承是多么的难懂。如哲学
总结:实例.. 这里不加super("aaa",32); 运行:父类和子类的姓名,年龄是一样的.那这个super为什么没效果呢? 显示:class:Ji姓名是 小红年龄是:20课程 ...