难度 medium
给你一个变量对数组 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 由小写英文字母与数字组成
解题思路:这里难度标的是medium,不过感觉是一道hard题目,这也是我在leetcode通过的第500道题,留个纪念,看的官方题解,然后自己写出来的,基本和官方题解一致了,主要是实现一个并查集的数据结构,并且实现主要的操作如找根,合并等等,感觉可以多看几次,加深印象吧,官方题解花了很多图,方便理解构建出来的有向图,这里就不把解题思路重新写一遍了,都一样的。
代码 t93 s94 java
class Solution {
public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
int equation_size = equations.size();
HashMap<String, Integer> map = new HashMap<>(2*equation_size);
UnionFind unionFind = new UnionFind(2 * equation_size);
int id = 0;
for(int i=0; i<equation_size; i++){
List<String> equation = equations.get(i);
String s1 = equation.get(0);
String s2 = equation.get(1);
if(!map.containsKey(s1)) map.put(s1, id++);
if(!map.containsKey(s2)) map.put(s2, id++);
unionFind.union(map.get(s1), map.get(s2), values[i]);
}
int query_size = queries.size();
double[] res = new double[query_size];
for(int i=0; i<query_size; i++){
String s1 = queries.get(i).get(0);
String s2 = queries.get(i).get(1);
if(!map.containsKey(s1) || !map.containsKey(s2)) res[i] = -1.0d;
else res[i] = unionFind.isConnect(map.get(s1), map.get(s2));
}
return res;
}
private class UnionFind{
private int[] parents;
private double[] weights;
public UnionFind(int n){
parents = new int[n];
weights = new double[n];
for(int i=0; i<n; i++){
parents[i] = i;
weights[i] = 1.0d;
}
}
public int find(int x){
if(x!=parents[x]){
int origin = parents[x];
parents[x] = find(parents[x]);
weights[x] *= weights[origin];
}
return parents[x];
}
public void union(int x, int y, double value){
int root_x = find(x);
int root_y = find(y);
if(root_x==root_y){
return;
}
parents[root_x] = root_y;
weights[root_x] = weights[y] * value / weights[x];
}
public double isConnect(int x, int y){
int root_x = find(x);
int root_y = find(y);
if(root_x==root_y) return weights[x] / weights[y];
else return -1.0d;
}
}
}
原文:https://www.cnblogs.com/zhengxch/p/14720034.html