-

题目: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-并查集的更多相关文章

  1. [leetcode] 并查集(Ⅰ)

    预备知识 并查集 (Union Set) 一种常见的应用是计算一个图中连通分量的个数.比如: a e / \ | b c f | | d g 上图的连通分量的个数为 2 . 并查集的主要思想是在每个连 ...

  2. [leetcode] 并查集(Ⅱ)

    最长连续序列 题目[128]:链接. 解题思路 节点本身的值作为节点的标号,两节点相邻,即允许合并(x, y)的条件为x == y+1 . 因为数组中可能会出现值为 -1 的节点,因此不能把 root ...

  3. [leetcode] 并查集(Ⅲ)

    婴儿名字 题目[Interview-1707]:典型并查集题目. 解题思路 首先对 names 这种傻 X 字符串结构进行预处理,转换为一个 map,key 是名字,val 是名字出现的次数. 然后是 ...

  4. Leetcode之并查集专题-765. 情侣牵手(Couples Holding Hands)

    Leetcode之并查集专题-765. 情侣牵手(Couples Holding Hands) N 对情侣坐在连续排列的 2N 个座位上,想要牵到对方的手. 计算最少交换座位的次数,以便每对情侣可以并 ...

  5. Leetcode之并查集专题-684. 冗余连接(Redundant Connection)

    Leetcode之并查集专题-684. 冗余连接(Redundant Connection) 在本问题中, 树指的是一个连通且无环的无向图. 输入一个图,该图由一个有着N个节点 (节点值不重复1, 2 ...

  6. 【LeetCode】并查集 union-find(共16题)

    链接:https://leetcode.com/tag/union-find/ [128]Longest Consecutive Sequence  (2018年11月22日,开始解决hard题) 给 ...

  7. Leetcode题目200.岛屿数量(BFS+DFS+并查集-中等)

    题目描述: 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 ...

  8. leetcode 76 dp& 强连通分量&并查集经典操作

    800. Similar RGB Color class Solution { int getn(int k){ return (k+8)/17; } string strd(int k){ char ...

  9. Java实现 LeetCode 721 账户合并(并查集)

    721. 账户合并 给定一个列表 accounts,每个元素 accounts[i] 是一个字符串列表,其中第一个元素 accounts[i][0] 是 名称 (name),其余元素是 emails ...

  10. C#LeetCode刷题-并查集

    并查集篇 # 题名 刷题 通过率 难度 128 最长连续序列   39.3% 困难 130 被围绕的区域   30.5% 中等 200 岛屿的个数   38.4% 中等 547 朋友圈   45.1% ...

随机推荐

  1. iftop简单使用

    在linux下想查看当前与主机通信的IP有哪些?流量多少?怎么办?使用iftop吧,小巧实用的小工具.在排查问题的时候能起到大作用. centos安装 yum install iftop 界面如下: ...

  2. 设置ll命令

    ll 是 ls -l的别名,之所以 ll出现错误是因为没有定义别名. 如果要实现ll 命令,可以做如下操作: 1.编辑 ~./bashrc 添加 ls -l 的别名为 ll即可. vi /root/. ...

  3. webpack最基本的用法

    webpack 安装 webpack是所以Node.js开发的工具,可通过npm安装,首先要保证node已经安装完毕,可以去node官网下载, 然后通过npm下载webpack npm install ...

  4. 基础课(一)Cisco Packet Tracer Student模拟器简单的运用

    一.相同设备之间用交叉线,不同设备之间用直通线 如上图中的简单的两个PC机相连,交叉线相连的两个PC机是能ping同,直通线相连的两台PC机不通,ipconfig可以查看本机设备的网络配置 time= ...

  5. xlwings结合dataframe数据的写入

    一.代码 import xlwings as xw import pandas as pd xl_path=r'***' df_path=r'***' df=pd.read_excel(df_path ...

  6. centos7安装kylo0.10.1

    安装环境centos7,kylo版本0.10.1 常用的链接地址 kylo官网:https://kylo.io/ kylo文档:https://kylo.readthedocs.io/ 下载地址 官网 ...

  7. jmeter之-用Firefox录制https协议证书问题

    录制脚本的时候,比如录制https协议的百度网站 https://www.baidu.com ,所有录制设置均正常,但是在jmeter录制控制器里面就是没有任何录制的请求. 这个时候提示说证书不对 1 ...

  8. js中的函数声明和函数表达式的区别

    目录 一.声明与表达式的格式 1.1 声明式的格式: 1.2 表达式的格式: 二.区别 2.1 函数表达式可以直接在后面加括号执行,而函数声明不可以. 2.2 函数表达式可以被提前解析出来 2.3 命 ...

  9. 炼数成金数据分析课程---10、python中如何画图

    炼数成金数据分析课程---10.python中如何画图 一.总结 一句话总结: 主要matplotlib库,pandas中也可以画一些基础图 大纲+实例快速学习法 1.matplotlib的最简单画图 ...

  10. 配置Maven私服

    Nexus 是“开箱即用”的系统,不需要数据库,它使用文件系统加 Lucene 来组织数据,支持 WebDAV 与 LDAP 安全身份认证.Nexus 还提供了强大的仓库管理功能,构件搜索功能,它基于 ...