[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) 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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- 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 用了第一种方式, ...
- 【LeetCode】694. Number of Distinct Islands 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...
- [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 ...
- 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 ...
- [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 ...
- [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 ...
随机推荐
- 布式实时日志系统(三) 环境搭建之centos 6.4下hadoop 2.5.2完全分布式集群搭建最全资料
最近公司业务数据量越来越大,以前的基于消息队列的日志系统越来越难以满足目前的业务量,表现为消息积压,日志延迟,日志存储日期过短,所以,我们开始着手要重新设计这块,业界已经有了比较成熟的流程,即基于流式 ...
- C#生成随机验证码例子
C#生成随机验证码例子: 前端: <tr> <td width=" align="center" valign="top"> ...
- Root用户安装MariaDB到 /usr/local/mysql
参考地址: http://www.zhdba.com/mysqlops/2013/08/16/mariadb-cn_1001/ https://mariadb.com/kb/en/mariadb/in ...
- 时间的类型的相互转换(date/String)及时区的比较
String ->Date ->String @Test public void date() throws ParseException{ String sdate = "01 ...
- [原]openstack-kilo--issue(三) openstack-nova-issue-systemctl start openstack-nova-compute.service
本博客已经添加"打赏"功能,"打赏"位置位于右边栏红色框中,感谢您赞助的咖啡. execute systemctl start openstack-nova-c ...
- win10 与linux mint双系统 只能进入mint而无法进入windows的解决方案
新购买了一块ssd,和以前的hdd硬盘一起装双系统:win10和mint ssd:win10 sdb1 sdb2 sdb3 sda2 hdd: mint sda1 ...
- 移动端前端框架UI库
移动端前端框架UI库(Frozen UI.WeUI.SUI Mobile) Frozen UI 自述:简单易用,轻量快捷,为移动端服务的前端框架. 主页:http://frozenui.github. ...
- unity3d的优化场景技术LOD+IOC
一.unity3d的优化场景技术 LOD+IOC 遮挡剔除(occlusion culling)其实就是在摄像机范围内的物体才被渲染出来,没有在视野范围内的,统统关掉渲染,这样能让性能大大提高. I ...
- Unity性能优化之Draw Call(转)
Unity(或者说基本所有图形引擎)生成一帧画面的处理过程大致可以这样简化描述:引擎首先经过简单的可见性测试,确定摄像机可以看到的物体,然后把这些物体的顶点(包括本地位置.法线.UV等),索引(顶点如 ...
- Mac - MySQL初始密码忘记重置MySQL root密码
在什么情况下,需要重置root密码呢?那就是我们忘记了.还有一种比较坑的,那就是笔者的这种情况.按照正常的情况下,MySQL安装完之后,会弹出一个对话框,显示着一个临时的root密码,但无论笔者如何重 ...