博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode每日一题【399. 除法求值】==》并查集&模板
阅读量:3973 次
发布时间:2019-05-24

本文共 3995 字,大约阅读时间需要 13 分钟。

文章目录

罪恶的源泉

给你一个变量对数组 equations 和一个实数值数组 values 作为已知条件,其中 equations[i] = [Ai, Bi]

和 values[i] 共同表示等式 Ai / Bi = values[i] 。每个 Ai 或 Bi 是一个表示单个变量的字符串。

另有一些以数组 queries 表示的问题,其中 queries[j] = [Cj, Dj] 表示第 j 个问题,请你根据已知条件找出 Cj

/ Dj = ? 的结果作为答案。

返回 所有问题的答案 。如果存在某个无法确定的答案,则用 -1.0 替代这个答案。

注意:输入总是有效的。你可以假设除法运算中不会出现除数为 0 的情况,且不存在任何矛盾的结果。

示例 1:

输入:equations = [[“a”,“b”],[“b”,“c”]], values = [2.0,3.0], queries =

[[“a”,“c”],[“b”,“a”],[“a”,“e”],[“a”,“a”],[“x”,“x”]]
输出:[6.00000,0.50000,-1.00000,1.00000,-1.00000] 解释: 条件:a / b = 2.0, b /
c = 3.0 问题:a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ?
结果:[6.0, 0.5, -1.0, 1.0, -1.0 ]

示例 2:

输入:equations = [[“a”,“b”],[“b”,“c”],[“bc”,“cd”]], values =

[1.5,2.5,5.0], queries = [[“a”,“c”],[“c”,“b”],[“bc”,“cd”],[“cd”,“bc”]]
输出:[3.75000,0.40000,5.00000,0.20000]

示例 3:

输入:equations = [[“a”,“b”]], values = [0.5], queries =

[[“a”,“b”],[“b”,“a”],[“a”,“c”],[“x”,“y”]]
输出:[0.50000,2.00000,-1.00000,-1.00000]

提示:

1 <= equations.length <= 20equations[i].length == 21 <= Ai.length, Bi.length <= 5values.length == equations.length0.0 < values[i] <= 20.01 <= queries.length <= 20queries[i].length == 21 <= Cj.length, Dj.length <= 5Ai, Bi, Cj, Dj 由小写英文字母与数字组成

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/evaluate-division
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

一、简介

在这里插入图片描述

二、模板

1.没有权重的并查集模板

class UnionFind:    def __init__(self):        """        记录每个节点的父节点        """        self.father = {
} def find(self,x): """ 查找根节点 路径压缩 """ root = x while self.father[root] != None: root = self.father[root] # 路径压缩 while x != root: original_father = self.father[x] self.father[x] = root x = original_father return root def merge(self,x,y,val): """ 合并两个节点 """ root_x,root_y = self.find(x),self.find(y) if root_x != root_y: self.father[root_x] = root_y def is_connected(self,x,y): """ 判断两节点是否相连 """ return self.find(x) == self.find(y) def add(self,x): """ 添加新节点 """ if x not in self.father: self.father[x] = None

2.有权重的模板

被除数当作子节点,除数当作父节点。

class UnionFind:    def __init__(self):        """        记录每个节点的父节点        记录每个节点到根节点的权重        """        self.father = {
} self.value = {
} def find(self,x): """ 查找根节点 路径压缩 更新权重 """ root = x # 节点更新权重的时候要放大的倍数 base = 1 while self.father[root] != None: root = self.father[root] base *= self.value[root] while x != root: original_father = self.father[x] ##### 离根节点越远,放大的倍数越高 self.value[x] *= base base /= self.value[original_father] ##### self.father[x] = root x = original_father return root def merge(self,x,y,val): """ 合并两个节点 """ root_x,root_y = self.find(x),self.find(y) if root_x != root_y: self.father[root_x] = root_y ##### 四边形法则更新根节点的权重 self.value[root_x] = self.value[y] * val / self.value[x] def is_connected(self,x,y): """ 两节点是否相连 """ return x in self.value and y in self.value and self.find(x) == self.find(y) def add(self,x): """ 添加新节点,初始化权重为1.0 """ if x not in self.father: self.father[x] = None self.value[x] = 1.0 class Solution: def calcEquation(self, equations: List[List[str]], values: List[float], queries: List[List[str]]) -> List[float]: uf = UnionFind() for (a,b),val in zip(equations,values): uf.add(a) uf.add(b) uf.merge(a,b,val) res = [-1.0] * len(queries) for i,(a,b) in enumerate(queries): if uf.is_connected(a,b): res[i] = uf.value[a] / uf.value[b] return res
你可能感兴趣的文章
assertion 'GTK_IS_WIDGET (widget)' failed的解决办法
查看>>
Ubuntu登录管理员账户时,输入密码后一直在登录界面循环
查看>>
Linux下的定时器以及POSIX定时器:timer_settime()
查看>>
POSIX定时器timer_create()以及线程中的gettid() 和pthread_self()
查看>>
c /c++中日期和时间的获取:strftime()函数
查看>>
C语言 回调函数
查看>>
c语言swap(a,b)值交换的4种实现方法
查看>>
C++小知识点
查看>>
【转载】zedboard中PL_GPIO控制(8个sw、8个leds)
查看>>
zedboard烧写程序到FLASH,用于QSPI Flash启动
查看>>
软件工程师,你必须知道的20个常识
查看>>
常用STL算法2_查找
查看>>
常用STL算法3_排序
查看>>
常用STL算法4_拷贝和替换
查看>>
STL综合案例
查看>>
O(logn)时间复杂度求Fibonacci数列
查看>>
【转】腾讯十年运维老兵:运维团队的五个“杀手锏”
查看>>
Iterator_traits
查看>>
Zedboard中的SPI通信记录文档(已实现)
查看>>
Android 发布到google Play的app搜索不到问题的解决
查看>>