leetcode-并查集
-
题目:130

并查集:
class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
f = {}
def find(x):
f.setdefault(x,x)
if f[x]!=x:
f[x] = find(f[x])
return f[x]
def union(x,y):
f[find(y)] = find(x)
if not board or not board[0]:
return
row,col = len(board),len(board[0])
dummy = row*col
for i in range(row):
for j in range(col):
if board[i][j] == "O":
if i == 0 or i == row - 1 or j == 0 or j == col - 1:
union(i * col + j, dummy)
else:
for x, y in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
if board[i + x][j + y] == "O":
union(i * col + j, (i + x) * col + (j + y)) for i in range(row):
for j in range(col):
if find(dummy) == find(i * col + j):
board[i][j] = "O"
else:
board[i][j] = "X"
题200:

并查集
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
f = {}
def find(x):
f.setdefault(x,x)
if f[x]!=x:
f[x] = find(f[x])
return f[x]
def union(x,y):
f[find(y)] = find(x) if not grid:
return 0
row,col =len(grid),len(grid[0])
for i in range(row):
for j in range(col):
if grid[i][j] == "":
for x, y in [[-1, 0], [0, -1]]:
tmp_i = i + x
tmp_j = j + y
if 0 <= tmp_i < row and 0 <= tmp_j < col and grid[tmp_i][tmp_j] == "":
union(tmp_i * col + tmp_j, i * col + j)
res = set()
for i in range(row):
for j in range(col):
if grid[i][j] == "":
res.add(find(col*i+j))
return len(res)
题目684:


方法:并查集 O(N) O(N)

from collections import defaultdict
class Solution:
def findRedundantConnection(self, edges: List[List[int]]) -> List[int]:
f = {}
def find(x):
f.setdefault(x,x)
if f[x] != x:
f[x] = find(f[x])
return f[x]
def union(x,y):
if find(x) != find(y):
f[find(y)] = find(x)
for x,y in edges:
if find(x) == find(y):
return [x,y]
else:
union(x,y)

另:

class Solution:
def findRedundantConnection(self, edges: List[List[int]]) -> List[int]:
p = {i: {i} for i in range(1, len(edges) + 1)} #并查集初始化
for x, y in edges:
if p[x] is not p[y]: #如果两个集合地址不一样
p[x] |= p[y] #合并集合
for z in p[y]:
p[z] = p[x] #修改元素集合标记的指针地址
else:
return [x, y]
题685


并查集;
class Solution:
def findRedundantDirectedConnection(self, edges: List[List[int]]) -> List[int]:
def find(f,x):
f.setdefault(x,x)
if f[x] != x:
f[x] = find(f,f[x])
return f[x]
def cycle(graph):
f = {}
for x,y in graph:
if find(f,x) == find(f,y):
return True
else:
f[find(f,y)] = find(f,x)
indegree = {i:0 for i in range(1,len(edges)+1)}
tmp = 0
for i,j in edges:
indegree[j] += 1
if indegree[j] == 2:
tmp = j
break
if tmp == 0:
f = {}
for x,y in edges:
if find(f,x) == find(f,y):
return [x,y]
else:
f[find(f,y)] = find(f,x)
else:
for x,y in edges[::-1]:
if y == tmp:
if not cycle(edges[:edges.index([x,y])]+edges[edges.index([x,y])+1:]) :
return [x,y]
leetcode-并查集的更多相关文章
- [leetcode] 并查集(Ⅰ)
预备知识 并查集 (Union Set) 一种常见的应用是计算一个图中连通分量的个数.比如: a e / \ | b c f | | d g 上图的连通分量的个数为 2 . 并查集的主要思想是在每个连 ...
- [leetcode] 并查集(Ⅱ)
最长连续序列 题目[128]:链接. 解题思路 节点本身的值作为节点的标号,两节点相邻,即允许合并(x, y)的条件为x == y+1 . 因为数组中可能会出现值为 -1 的节点,因此不能把 root ...
- [leetcode] 并查集(Ⅲ)
婴儿名字 题目[Interview-1707]:典型并查集题目. 解题思路 首先对 names 这种傻 X 字符串结构进行预处理,转换为一个 map,key 是名字,val 是名字出现的次数. 然后是 ...
- Leetcode之并查集专题-765. 情侣牵手(Couples Holding Hands)
Leetcode之并查集专题-765. 情侣牵手(Couples Holding Hands) N 对情侣坐在连续排列的 2N 个座位上,想要牵到对方的手. 计算最少交换座位的次数,以便每对情侣可以并 ...
- Leetcode之并查集专题-684. 冗余连接(Redundant Connection)
Leetcode之并查集专题-684. 冗余连接(Redundant Connection) 在本问题中, 树指的是一个连通且无环的无向图. 输入一个图,该图由一个有着N个节点 (节点值不重复1, 2 ...
- 【LeetCode】并查集 union-find(共16题)
链接:https://leetcode.com/tag/union-find/ [128]Longest Consecutive Sequence (2018年11月22日,开始解决hard题) 给 ...
- Leetcode题目200.岛屿数量(BFS+DFS+并查集-中等)
题目描述: 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 ...
- leetcode 76 dp& 强连通分量&并查集经典操作
800. Similar RGB Color class Solution { int getn(int k){ return (k+8)/17; } string strd(int k){ char ...
- Java实现 LeetCode 721 账户合并(并查集)
721. 账户合并 给定一个列表 accounts,每个元素 accounts[i] 是一个字符串列表,其中第一个元素 accounts[i][0] 是 名称 (name),其余元素是 emails ...
- C#LeetCode刷题-并查集
并查集篇 # 题名 刷题 通过率 难度 128 最长连续序列 39.3% 困难 130 被围绕的区域 30.5% 中等 200 岛屿的个数 38.4% 中等 547 朋友圈 45.1% ...
随机推荐
- 2019-9-2-C#委托
title author date CreateTime categories C#委托 lindexi 2019-09-02 12:57:37 +0800 2018-2-13 17:23:3 +08 ...
- git 分支相关操作
git status 查看当前工作区 会显示分支 如下 D:\工程\vue_study\testplat_vue>git statusOn branch masternothing to co ...
- Java工程师面试linux操作选择面试题大全
1.请写出常用的linux指令不低于10个,请写出linux tomcat启动.linux指令arch 显示机器的处理器架构(1)uname -m 显示机器的处理器架构(2)shutdown -h n ...
- 通过实例详细讲解PHP垃圾回收机制
PHP垃圾回收机制:1. PHP可以自动进行内存管理,清除不需要的对象,主要使用了引用计数2. 在zval结构体中定义了ref_count和is_ref , ref_count是引用计数 ,标识此zv ...
- 骚操作:c++如何用goto便捷地写人工栈?
在如今所有NOI系列赛事已经开全栈的时势下,人工栈已经离我们很远很远. 所以这博客就是我弄着玩的. 首先我们要清楚的是c++的goto写法: loop:; - goto loop; 在运行到goto时 ...
- Yii2中一些常用的参数
系统类型: <?= php_uname() ?> 解译引擎: <?= $_SERVER['SERVER_SOFTWARE'] ?>, Zend: <?= Zend_Ver ...
- Linux环境下安装PHP的mbstring模块
cd /home/local/php-5.6.25/ext/mbstring/usr/local/php/bin/phpize./configure --with-php-config=/usr/lo ...
- 一些DP杂题
1. [HNOI2001] 产品加工 一道简单的背包,然而我还是写了很久QAQ 时间范围是都小于5 显然考虑一维背包,dp[i]表示目前A消耗了i的最小B消耗 注意 if(b[i]) dp[j]=dp ...
- tp5使用jwt生成token,做api的用户认证
首先 composer 安装 firebase/php-jwt github:https://github.com/firebase/php-jwt composer require firebas ...
- 用 Flask 来写个轻博客 (1) — 创建项目
目录 目录 前言 扩展阅读 部署开发环境 创建 Github 项目 前言 一步一步的实现一个 Flask 轻博客项目启动,最新的代码会上传到 Github. 扩展阅读 欢迎使用 Flask - vir ...