[抄题]:

Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.

Count the number of distinct islands. An island is considered to be the same as another if and only if one island can be translated (and not rotated or reflected) to equal the other.

Example 1:

11000
11000
00011
00011

Given the above grid map, return 1.

Example 2:

11011
10000
00001
11011

Given the above grid map, return 3.

Notice that:

11
1

and

 1
11

are considered different island shapes, because we do not consider reflection / rotation.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

不知道怎么存一块岛屿的形状:其实就是存方向构成的string就行了

每次走过一个点,都要设置该点为0,因为形状只统计一次,不重复

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

DFS就是:走到底-到最后一行改变条件-没出界就再走到底,反正就是走到底就对了

          {1, 1, 0},
{0, 1, 1},
{0, 0, 0},
{1, 1, 1},
{0, 1, 0}
为了分辨方向序列,加星号的位置不同。
curShape = rdr
curShape = rdr*
curShape = rdr**
curShape = rdr***
curShape = rd
curShape = rd*r
curShape = rd*r*
curShape = rd*r**
 

[一刷]:

  1. stringbuilder需要专门的append函数而不是加号来连接string,所以在dfs中的参数就是它本身

[二刷]:

  1. if grid[i][j] = 1后从主函数进去就行,dfs不用再写一次了

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

[复杂度]:Time complexity: O(mn) Space complexity: O(mn)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

class Solution {
public int numDistinctIslands(int[][] grid) {
//corner cases
if (grid == null || grid.length == 0 || grid[0].length == 0) return 0; //initialization: StringBuilder, hashset
StringBuilder curShape;
Set<String> set = new HashSet<String>(); //start to expand if grid[i][j] == 1
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == 1) {
curShape = new StringBuilder(); expandIslands(grid, i, j, curShape, "");
//add to set after trimed
set.add(curShape.toString().trim());
}
}
} //return keyset
return set.size();
} public void expandIslands(int[][] grid, int x, int y, StringBuilder curShape, String directions) {
//exit case
if (grid[x][y] == 0 || x < 0 || x >= grid.length || y < 0 || y >= grid[0].length) return ; //set grid[x][y] = 0
grid[x][y] = 0;
//append the cur directions
curShape.append(directins); //expand in 4 directions
expandIslands(grid, x - 1, y, curShape, "U");
expandIslands(grid, x + 1, y, curShape, "D");
expandIslands(grid, x, y + 1, curShape, "R");
expandIslands(grid, x, y - 1, curShape, "L"); //add a * if neccessary
curShape.append(" "); }
}

694. Number of Distinct Islands 形状不同的岛屿数量的更多相关文章

  1. leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions

    两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...

  2. [LeetCode] 694. Number of Distinct Islands 不同岛屿的个数

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  3. 【LeetCode】694. Number of Distinct Islands 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...

  4. [leetcode]694. Number of Distinct Islands你究竟有几个异小岛?

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  5. [LeetCode] 694. Number of Distinct Islands

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  6. [LeetCode] 711. Number of Distinct Islands II_hard tag: DFS

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  7. [LeetCode] Number of Distinct Islands II 不同岛屿的个数之二

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  8. [LeetCode] Number of Distinct Islands 不同岛屿的个数

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  9. [LeetCode] 711. Number of Distinct Islands II 不同岛屿的个数之二

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

随机推荐

  1. 解决Android Studio在Ubuntu上出现“sdk/platform-tools/adb: error=2, No such file or directory”的方法

    转载至http://blog.163.com/china_uv/blog/static/11713726720136931132385/ 刚安装Ubuntu14.5时运行Android Studio可 ...

  2. VDMA时序分析

    VDMA时序分析

  3. Python递归解压缩多级.zip压缩包

    参考如下代码(from:https://stackoverflow.com/questions/36285502/how-to-extract-zip-file-recursively-in-pyth ...

  4. NetCore2.0 CodeFirst 解析全国区划信息

    NetCore2.0 数据库:SQLite HTML解析:HtmlAgilityPack 区划数据:http://www.stats.gov.cn/tjsj/tjbz/xzqhdm/201703/t2 ...

  5. *浅解嵌入式中的BootLoader

    本文只作为本人学习过程中的记录及时不时的突发奇想偶记.鄙人菜鸟一只,文中如有错误或疏漏,若读者肯不吝赐教,在下感激零涕.文章一直不断更新中 一.何为Bootloader 在嵌入式系统中,Bootloa ...

  6. 简单的知识图谱,neo4j+python

    因为研究方向是知识图谱,就有兴致想要构建一个简单的知识图谱,就在网上查找了一下,参考了neo4j搭建简单的金融知识图谱的思想,就着手从零开始构建. 1.首先就要考虑数据的获得,因为之前没有接触过爬虫之 ...

  7. crossdomain.xml配置不当的利用和解决办法

    00x1: 今天在无聊的日站中发现了一个flash小站,点进crossdomain.xml一看,震惊 本屌看到这个*就发觉事情不对 百度一下,这是一个老洞,配置不当能引起各种问题就算能远程加载恶意的s ...

  8. 20165312 2017-2018-2 《JAVA程序设计》第2周学习总结

    20165312 2017-2018-2 <JAVA程序设计>第2周学习总结 一.对上一周学习的查漏补缺 1.上周在虚拟机中进行编译程序时出现错误,在上一周的博客中我有提到,当时还未找到解 ...

  9. css变换与动画详解

    举个栗子:--------元素整体居中.box{     position:absolute;top:50%;left:50%;    width:50px;    height:50px;    t ...

  10. Java内存管理之类似-Xms、-Xmx 这些参数的含义

    1.堆内存分配:JVM 初始分配的内存由**-Xms** 指定,默认是物理内存的 1/64:JVM 最大分配的内存由**-Xmx** 指定,默认是物理内存的 1/4:默认空余堆内存小于 40% 时,J ...