当前位置:网站首页>LeetCode_并查集_中等_399. 除法求值
LeetCode_并查集_中等_399. 除法求值
2022-06-10 18:25:00 【一瓢江湖我沉浮】
1.题目
给你一个变量对数组 equations 和一个实数值数组 values 作为已知条件,其中 equations[i] = [Ai, Bi] 和 values[i] 共同表示等式 Ai / Bi = values[i] 。每个 Ai 或 Bi 是一个表示单个变量的字符串。
另有一些以数组 queries 表示的问题,其中 queries[j] = [Cj, Dj] 表示第 j 个问题,请你根据已知条件找出 Cj / Dj = ? 的结果作为答案。
返回所有问题的答案 。如果存在某个无法确定的答案,则用 -1.0 替代这个答案。如果问题中出现了给定的已知条件中没有出现的字符串,也需要用 -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 <= 20
equations[i].length == 2
1 <= Ai.length, Bi.length <= 5
values.length == equations.length
0.0 < values[i] <= 20.0
1 <= queries.length <= 20
queries[i].length == 2
1 <= Cj.length, Dj.length <= 5
Ai, Bi, Cj, Dj 由小写英文字母与数字组成
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/evaluate-division
2.思路
(1)并查集
思路参考本题官方题解。
相似题目:LeetCode_并查集_中等_990.等式方程的可满足性
3.代码实现(Java)
//思路1————并查集
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Solution {
public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
int equationsSize = equations.size();
UnionFind unionFind = new UnionFind(2 * equationsSize);
// 第 1 步:预处理,将变量的值与 id 进行映射,使得并查集的底层使用数组实现,方便编码
Map<String, Integer> hashMap = new HashMap<>(2 * equationsSize);
int id = 0;
for (int i = 0; i < equationsSize; i++) {
List<String> equation = equations.get(i);
String var1 = equation.get(0);
String var2 = equation.get(1);
if (!hashMap.containsKey(var1)) {
hashMap.put(var1, id);
id++;
}
if (!hashMap.containsKey(var2)) {
hashMap.put(var2, id);
id++;
}
unionFind.union(hashMap.get(var1), hashMap.get(var2), values[i]);
}
// 第 2 步:做查询
int queriesSize = queries.size();
double[] res = new double[queriesSize];
for (int i = 0; i < queriesSize; i++) {
String var1 = queries.get(i).get(0);
String var2 = queries.get(i).get(1);
Integer id1 = hashMap.get(var1);
Integer id2 = hashMap.get(var2);
if (id1 == null || id2 == null) {
res[i] = -1.0d;
} else {
res[i] = unionFind.isConnected(id1, id2);
}
}
return res;
}
private class UnionFind {
private int[] parent;
/** * 指向的父结点的权值 */
private double[] weight;
public UnionFind(int n) {
this.parent = new int[n];
this.weight = new double[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
weight[i] = 1.0d;
}
}
public void union(int x, int y, double value) {
int rootX = find(x);
int rootY = find(y);
if (rootX == rootY) {
return;
}
parent[rootX] = rootY;
// 关系式的推导请见「参考代码」下方的示意图
weight[rootX] = weight[y] * value / weight[x];
}
/** * 路径压缩 * * @param x * @return 根结点的 id */
public int find(int x) {
if (x != parent[x]) {
int origin = parent[x];
parent[x] = find(parent[x]);
weight[x] *= weight[origin];
}
return parent[x];
}
public double isConnected(int x, int y) {
int rootX = find(x);
int rootY = find(y);
if (rootX == rootY) {
return weight[x] / weight[y];
} else {
return -1.0d;
}
}
}
}
边栏推荐
- Low carbon data center construction ideas and future trends
- c指针(面试经典题目练习)
- Nodejs judge system type get host name execute console command Chinese garbled code
- 调试的技巧
- c(指针-02)
- Adobe Premiere基础-不透明度(蒙版)(十一)
- openSSL1.1.1编译错误 Can‘t locate Win32/Console.pm in @INC
- Chapter II data type (I)
- Adobe Premiere Foundation (track related) (V)
- Db2 SQL PL简介
猜你喜欢

我的第一部作品:TensorFlow2.x

基于SSM流量计量云系统的设计与实现.rar(论文+项目源码)
![[vulnhub range] janchow: 1.0.1](/img/b5/e3f0d213ee87cd60802ee3db79d10f.png)
[vulnhub range] janchow: 1.0.1

第二章 数据类型(一)

Libcurl 7.61.0 vs2013 compilation tutorial

Implementation analysis of single image haze removal using dark channel prior

【Vulnhub靶场】JANGOW: 1.0.1

libcurl 7.61.0 VS2013 编译教程

2022.05.26(LC_1143_最长公共子序列)

2022.05.27(LC_647_回文子串)
随机推荐
Adobe Premiere foundation - opacity (mixed mode) (XII)
mysql(17-觸發器)
2022.05.29(LC_6078_重排字符形成目标字符串)
Mysql8.0 (summary of new features)
Anchor type and row data type of DB2 SQL pl
第三章 数据类型(二)
Adobe Premiere foundation - material nesting (animation of Tiktok ending avatar) (IX)
端午“沉浸式云旅游”怎么玩?即构助力“直播+”新场景落地
Pits encountered during the use of ETL (ETL Chinese garbled)
Array type of DB2 SQL pl
【杂谈】恭喜自己获得CSDN专家称号,努力终会换来结果
VS从txt文件读取中文汉字产生乱码的解决办法(超简单)
Opencv does not rely on any third-party database for face detection
lingo12软件下载及lingo语言入门资源
c指针(面试经典题目练习)
Nodejs judge system type get host name execute console command Chinese garbled code
How to set up salesmartly for Google Analytics tracking
5. golang generics and reflection
openSSL1.1.1编译错误 Can‘t locate Win32/Console.pm in @INC
Adobe Premiere基础-时间重映射(十)