[抄题]:

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. ASP.NET 4.0 :MasterPage母版页的ClientIDMode属性

    在ASP.NET 4.0之前我们总是要为控件的ClientID头疼,比如明明一个叫lblName的Label放在一个叫做grd的GridView里面后,在页面上改Label的ID就变成了诸如grd_c ...

  2. 小程序 去掉 <button /> 组件默认 border 边框样式

    代码: <button class="btn">点击</button> 效果图: 目的:去掉 <button /> 默认样式中的 backgro ...

  3. VUE 高德地图选取地址组件开发

    高德地图文档地址 http://lbs.amap.com/api/lightmap/guide/picker/ 结合步骤: 1.通过iframe内嵌引入高德地图组件 key就选你自己申请的key &l ...

  4. 黄聪:C#使用Application.Restart重启程序出错解决办法

    调用 Application.Restart重启程序出错 解决办法,就是给程序的.exe文件,加上下面的设置

  5. <亲测>centos7通过yum安装JDK1.8(实际上是openjdk)

    centos7通过yum安装JDK1.8   安装之前先检查一下系统有没有自带open-jdk 命令: rpm -qa |grep java rpm -qa |grep jdk rpm -qa |gr ...

  6. Optaplanner - 从探究示例中的hello world,初步认识规划引擎的运行步骤。

    上一篇我们成功以把Opotaplanner规划引擎下载回来,并把它的示例运行起来,简单解析了一下它的Cloud balance示例.这一篇我们这些示例的源代码导入到Eclipse中,看看它在后台是怎么 ...

  7. repos配置

  8. 转:通过ASP.Net页面获取域用户名(当前登陆的用户)

    通过ASP.Net页面获取域用户名(当前登陆的用户) 原文地址: https://www.cnblogs.com/fast-michael/archive/2011/03/14/2057954.htm ...

  9. tips:Jquery的attr和prop的区别

    Jquery的attr和prop的区别 描述:想做一个复选框checkbox全选的功能,当勾选全选后,将子项的复选框状态设置成一致的, 但遇到了一个问题,就是attr函数并不能改变子项的checkbo ...

  10. node.js打印function

    var Person = function(name) { this.name = name; this.gender = ['man', 'woman']; } console.log(Person ...