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.

Note: The length of each dimension in the given grid does not exceed 50.

这个题目可以理解为是[LeetCode] 200. Number of Islands_ Medium tag: BFS的变形, 不仅仅是要判断有多少个island, 并且是要unique的, 不能翻转, 那么如果做呢, 因为我们scan的时候是一行一行得scan, 用DFS去scan的时候也有它的先后顺序, 所以可以通过每次用DFS的时候将路径记下来, 并且add进入一个ans的set里面, 最后返回len(ans), 就是distinct islands的数目了.

updated at 07/10, 参考题目的Solution, 思路跟之前类似, 只不过, 每个island我们将坐标都变为local的坐标, 相当于以最开始的坐标作为原点坐标, 获得相对坐标的位置, 然后将该路径记下来, 并且将其变为tuple然后add进入一个ans的set里面, 最后返回len(ans).

Updated at 08/17, 将local location的code update更加elegant.

1. Constraints

1) grid can be empty,

2) grid size <= 50 * 50

3) element will only be 0 or 1

2. Ideas

DFS:      T: O(m*n)      S: O(m*n)

1) edge case, not grid or len(grid[0]) == 0, return 0

2) grid[i][j] if not visited, dfs(得到它的路径), 并add进入ans 的set

3) 对于dfs, 记住路径, 并且append进入dirs

4) 返回len(ans)

3. Code

 class Solution:
def numDistinctIslands(self, grid):
if not gird or len(grid[0]) == 0: return 0
lr, lc, ans, visited = len(grid), len(grid[0]), set(), set() def dfs(grid, i, j, visited, dirs, dirt):
if 0<= i < len(grid) and 0 <= j < len(grid[0]) and grid[i][j] == 1 and (i, j) not in visited:
visited.add((i,j))
dirs.append(dirt)
dfs(grid, i-1, j, visited, dirs, 'u')
dfs(grid, i+1, j, visited, dirs, 'd')
dfs(grid, i, j-1, visited, dirs, 'l')
dfs(grid, i, j+1, visited, dirs, 'r')
dirs.append('b') # if no this line, [[110],[011],[000],[111],[010]] can not pass
for i in range(lr):
for j in range(lc):
if grid[i][j] == 1 and (i,j) not in visited:
dirs = []
dfs(grid, i, j,visited, dirs, 'o' )
ans.add("".join(dirs))
return len(ans)

3.1  Code(利用loca 相对坐标位置)

class Solution:
def numberDistinctIslands(self, grid):
if not grid or len(grid[0]) == 0: return 0
lrc, ans, visited = [len(grid), len(grid[0])], set(), set()
def dfs(i,j,i0,j0):
if 0 <= i < lrc[0] and 0 <= j < lrc[1] and grid[i][j] and (i,j) not in visited:
temp.append((i-i0, j-j0))
visited.add((i,j))
dfs(i-1,j,i0,j0)
dfs(i+1,j,i0,j0)
dfs(i,j+1,i0,j0)
dfs(i,j-1,i0,j0) for i in range(lrc[0]):
for j in range(lrc[1]):
temp = []
dfs(i,j,i,j)
if temp:
ans.add(tuple(temp)) # note: set 里面必须是tuple,不能是list。
return len(ans)

4. Test cases

1) edge cases

2) [[110],[011],[000],[111],[010]]

3)

11011
10000
00001
11011

[LeetCode] 694. Number of Distinct Islands的更多相关文章

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

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

  4. 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 用了第一种方式, ...

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

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

  6. [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 ...

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

  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] 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 反编译Apk提取XML文件

    Apktool https://ibotpeaches.github.io/Apktool/install/ 下载地址:Apktool https://bitbucket.org/iBotPeache ...

  2. 使用mimikatz获取和创建Windows凭据的工具和方法

    Mimikatz 下载地址 https://github.com/gentilkiwi/mimikatz/releases 本地凭据破解 以管理员身份运行(拿到shell提权后) mimikatz#p ...

  3. 其它终端设备连接gmail账户提示密码错误解决方法

    换新手机配置Google Account继续使用Gmail服务,输入用户名.密码进入状态同步一段时间后再次提示输入用户名.密码并显示账号信息不正确.网上有人提到"修改用户密码"再进 ...

  4. Pry的安装

    Pry 用于rails应用的调试 在Gemfile中添加 gem 'pry', :group =>:development bundle install 即可.pry代替irb方法,直接运行: ...

  5. oracle dblink 查询 tns:无法解析指定的连接标识符

    问题情景是这样的:我在数据库服务器(windows server 2008r2 ,64bit)oracle(11gr2,64bit)中通过dblink连接到另外一台服务器(hp-ux)的oracle( ...

  6. github使用密钥登录

    注册github之后 初次使用git的用户要使用git协议大概需要三个步骤: 一.生成密钥对 二.设置远程仓库(本文以github为例)上的公钥     一.生成密钥对 再window系统中可以通过x ...

  7. Ubuntu 16.04系统下开机提示“无法应用原保存的显示器配置”

    开机启动Ubuntu时,提示以下错误,部分截图如图: 解决方法: 按住Ctrl+Alt+T开启终端,输入rm .config/monitors.xml,回车,然后重启Ubuntu即可解决:如图

  8. 第一个springMVC入门程序

    先看下项目结构 要加载与spring相关的包 HelloController.java(注意是在包controller下) package controller; import org.springf ...

  9. vue之计算属性和侦听器

    一.计算属性 模板内的表达式非常便利,但是设计它们的初衷是用于简单运算的.在模板中放入太多的逻辑会让模板过重且难以维护.例如: <div> {{ message.split('').rev ...

  10. springMVC + quartz实现定时器(任务调度器)

    首先我们要知道任务调度器(定时器)有几种,这边我会写三种 第一种是基于JDK的本身的一个定时器(优点:简单,缺点:满足不了复杂的需求) package com.timer1; import java. ...