LeetCode题解(0711):不同岛屿的数量II(Python)
发布日期:2021-06-29 20:09:29
浏览次数:3
分类:技术文章
本文共 4349 字,大约阅读时间需要 14 分钟。
题目:(困难)
标签:哈希表、图、广度优先搜索、深度优先搜索
解法 | 时间复杂度 | 空间复杂度 | 执行用时 |
---|---|---|---|
Ans 1 (Python) | O ( N × M + I ) O(N×M+I) O(N×M+I) : 其中I为岛屿数量 | O ( I ) O(I) O(I) | 320ms (60.87%) |
Ans 2 (Python) | |||
Ans 3 (Python) |
解法一:
class Solution: def numDistinctIslands(self, grid: List[List[int]]): visited = set() islands = set() size1, size2 = len(grid), len(grid[0]) for i1 in range(size1): for i2 in range(size2): if grid[i1][i2] == 0 or (i1, i2) in visited: continue land, min1, min2, max1, max2 = { (i1, i2)}, i1, i2, i1, i2 pos_list = collections.deque([(i1, i2)]) while pos_list: i3, i4 = pos_list.popleft() if i3 + 1 < size1 and grid[i3 + 1][i4] == 1 and (i3 + 1, i4) not in land: land.add((i3 + 1, i4)) pos_list.append((i3 + 1, i4)) max1 = max(max1, i3 + 1) if i4 + 1 < size2 and grid[i3][i4 + 1] == 1 and (i3, i4 + 1) not in land: land.add((i3, i4 + 1)) pos_list.append((i3, i4 + 1)) max2 = max(max2, i4 + 1) if i3 - 1 >= 0 and grid[i3 - 1][i4] == 1 and (i3 - 1, i4) not in land: land.add((i3 - 1, i4)) pos_list.append((i3 - 1, i4)) min1 = min(min1, i3 - 1) if i4 - 1 >= 0 and grid[i3][i4 - 1] == 1 and (i3, i4 - 1) not in land: land.add((i3, i4 - 1)) pos_list.append((i3, i4 - 1)) min2 = min(min2, i4 - 1) island = [] for i3 in range(min1, max1 + 1): row = [] for i4 in range(min2, max2 + 1): if (i3, i4) in land: row.append("1") else: row.append("0") island.append("".join(row)) island = " ".join(island) visited |= land islands.add(island) return islands def numDistinctIslands2(self, grid: List[List[int]]) -> int: # 对角线翻转 def reverse_diagonal(source): source = source.split(" ") ss1, ss2 = len(source), len(source[0]) result = [] for ii1 in range(ss2): rr = [] for ii2 in range(ss1): rr.append(source[ii2][ii1]) result.append("".join(rr)) return " ".join(result) # 水平翻转 def reverse_horizontal(source): source = source.split(" ") ss1, ss2 = len(source), len(source[0]) result = [] for ii1 in range(ss1): rr = [] for ii2 in range(ss2): rr.append(source[ii1][ss2 - 1 - ii2]) result.append("".join(rr)) return " ".join(result) # 垂直翻转 def reverse_perpendicular(source): source = source.split(" ") ss1, ss2 = len(source), len(source[0]) result = [[] for _ in range(ss1)] for ii2 in range(ss2): for ii1 in range(ss1): result[ii1].append(source[ss1 - 1 - ii1][ii2]) str_result = [] for ii1 in range(ss1): str_result.append("".join(result[ii1])) return " ".join(str_result) # 所有可能 def all_change(source): yield source yield reverse_horizontal(source) yield reverse_perpendicular(source) yield reverse_horizontal(reverse_perpendicular(source)) yield reverse_diagonal(source) yield reverse_diagonal(reverse_horizontal(source)) yield reverse_diagonal(reverse_perpendicular(source)) yield reverse_diagonal(reverse_horizontal(reverse_perpendicular(source))) # 生成所有岛屿 islands = self.numDistinctIslands(grid) # 岛屿去重 islands_set = set() for island in islands: unique = True for change in all_change(island): if change in islands_set: unique = False break if unique: islands_set.add(island) return len(islands_set)
转载地址:https://dataartist.blog.csdn.net/article/details/109838094 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
不错!
[***.144.177.141]2024年04月30日 12时28分33秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
C#中ref和out的使用小结
2019-04-30
(C#)方法参数关键字:ref、out、params详解
2019-04-30
大话C#中in,out,ref的作用和区别
2019-04-30
IEnumerator和IEnumerable是怎样使用的
2019-04-30
类变量 索引器
2019-04-30
IEnumerable和IEnumerator 详解
2019-04-30
非泛型集合类以及对应的泛型集合类【dictionary】
2019-04-30
迭代器学习之一:使用IEnumerable和IEnumerator接口
2019-04-30
迭代器学习之二:数组的可枚举类型和枚举数的定义以及编译器的foreach工作原理
2019-04-30
迭代器学习之三:IEnumerable和IEnumerator的泛型结构
2019-04-30
迭代器学习之四:关于yield的深入了解
2019-04-30
ORA-02046: ORA-02046:分布式事务处理已经开始
2019-04-30
创建和读写文件的一些简单方法
2019-04-30
XmlDocument操作xml文档
2019-04-30
.Net那点事儿系列:C#操作Xml:通过XmlDocument读写Xml文档
2019-04-30
使用XmlDocument创建XML文档及增加删除更新节点
2019-04-30
XML: 使用XmlDocument 与 XmlReader 类
2019-04-30
NET ListView选中行的定位显示
2019-04-30