[抄题]:

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 

[一刷]:

  1. “连续统计”的题往往有准入门槛。必须第一个数符合条件,才能做后续的统计。

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼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岛屿周长的更多相关文章

  1. 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 ...

  2. 463 Island Perimeter 岛屿的周长

    详见:https://leetcode.com/problems/island-perimeter/description/ C++: class Solution { public: int isl ...

  3. [LeetCode] Island Perimeter 岛屿周长

    You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...

  4. 463. Island Perimeter - LeetCode

    Question 463. Island Perimeter Solution 题目大意:给出一个二维数组1表示陆地0表示海,求陆地的周长 思路: 重新构造一张地图grid2即一个二维数组,比原数组大 ...

  5. [LeetCode] 463. Island Perimeter 岛的周长

    You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...

  6. LeetCode 463. Island Perimeter (岛的周长)

    You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...

  7. Leetcode463.Island Perimeter岛屿的周长

    给定一个包含 0 和 1 的二维网格地图,其中 1 表示陆地 0 表示水域. 网格中的格子水平和垂直方向相连(对角线方向不相连).整个网格被水完全包围,但其中恰好有一个岛屿(或者说,一个或多个表示陆地 ...

  8. LeetCode 463 Island Perimeter 解题报告

    题目要求 You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 rep ...

  9. 463. Island Perimeter

    https://leetcode.com/problems/island-perimeter/ 在一个N×N的矩阵中,N<100,1代表岛,0代表海,岛内没有海,求岛的周长 [[0,1,0,0] ...

随机推荐

  1. java中base64

    // 将 s 进行 BASE64 编码 public static String getBASE64(String s) { if (s == null) return null; return (n ...

  2. linux IP局域网监控工具——iptraf

    iptraf iptraf是一款交互式.色彩鲜艳的IP局域网监控工具.它可以显示每个连接以及主机之间传输的数据量.下面是屏幕截图. $ sudo iptraf 安装iptraf: # Centos(基 ...

  3. fabric 安装及简单使用 (centos6)

    fabric 是一个python的库,fabric可以通过ssh批量管理服务器. 第一步安装依赖包 安装epel源 1 wget -O /etc/yum.repos.d/epel.repo http: ...

  4. hbase_异常_05_End of File Exception between local host is: "rayner/127.0.1.1"; destination host is: "localhost":9000;

    一.异常信息 java.io.EOFException: End of File Exception between local host is: "ubuntu/127.0.1.1&quo ...

  5. MySQL管理

    http://www.yiibai.com/mysql/administration.html 在本节中,您将学习有关MySQL管理教程,包括MySQL服务器启动和关闭,MySQL服务器安全性,MyS ...

  6. Shell check IP

    #! /bin/bash checkip() {        if echo $1 |egrep -q '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3 ...

  7. mac快捷键整理以及node的基本使用

    该文章是作为日常积累和整理,又是好久没有整理node的相关知识了,最近翻了翻自己的有道云笔记,怎一个乱字了的,重新整理下. 一.Mac常用快捷键 Command+M: 最小化窗口 Command+T: ...

  8. HTML实用案例(1)—— 左侧菜单,右侧内容的布局(带左侧菜单点击隐藏显示效果)

    效果图 代码部分 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" ...

  9. mxnet(gluon)—— 模型、数据集、损失函数、优化子等类、接口大全

    1. 数据集 dataset_train = gluon.data.ArrayDataset(X_train, y_train) data_iter = gluon.data.DataLoader(d ...

  10. RedHat 6.8 打开vga之后Login界面花屏

    /******************************************************************* * RedHat 6.8 打开vga之后Login界面花屏 * ...