399. 除法求值(并查集)
作者:互联网
399. 除法求值
给你一个变量对数组 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
由小写英文字母与数字组成
1 class Solution { 2 public: 3 vector<int> parent; // 存储当前节点的根节点 4 vector<double> weight; // 存储当前节点到根节点的权值 5 void initUnionFind(int n) { 6 parent.resize(n); 7 weight.resize(n); 8 for (int i = 0; i < n; i++) { 9 parent[i] = i; 10 weight[i] = 1; // 自己与自己的商为1 11 } 12 } 13 // 获取x与y的商 14 double getTheQuotientOfTwoNumbers(int x, int y) { 15 int xRoot = findRoot(x); 16 int yRoot = findRoot(y); 17 // 如果x与y在同一集合中,则获取x与y的商,否则不能获取到商 18 if (xRoot == yRoot){ 19 return (weight[x] / weight[y]); 20 } else { 21 return -1.0; 22 } 23 } 24 void unify(int x, int y, double value) { 25 int xRoot = findRoot(x); 26 int yRoot = findRoot(y); 27 if (xRoot == yRoot) { 28 return; 29 } 30 parent[xRoot] = yRoot; 31 weight[xRoot] = (weight[y] * value) / weight[x]; 32 return; 33 } 34 int findRoot(int x) { 35 if (x != parent[x]) { 36 int next = parent[x]; 37 parent[x] = findRoot(next); // 递归调用找到x父节点的根节点 38 weight[x] *= weight[next]; // 查找时更新节点到根节点的权值 39 x = next; 40 } 41 return parent[x]; 42 } 43 vector<double> calcEquation(vector<vector<string>>& equations, vector<double>& values, vector<vector<string>>& queries) { 44 int n = equations.size(); 45 initUnionFind(2 * n); 46 unordered_map<string, int> hashMap; // 存储小写字母与数字的映射关系 47 int id = 0; 48 for (unsigned int i = 0; i < equations.size(); i++) { 49 string str1 = equations[i][0]; 50 string str2 = equations[i][1]; 51 if (hashMap.count(str1) <= 0) { 52 hashMap[str1] = id; 53 id++; 54 } 55 if (hashMap.count(str2) <= 0) { 56 hashMap[str2] = id; 57 id++; 58 } 59 unify(hashMap[str1], hashMap[str2], values[i]); 60 } 61 // 查询操作 62 vector<double> ans; 63 for (unsigned int i = 0; i < queries.size(); i++) { 64 string str1 = queries[i][0]; 65 string str2 = queries[i][1]; 66 if (hashMap.count(str1) > 0 && hashMap.count(str2) > 0) { 67 ans.push_back(getTheQuotientOfTwoNumbers(hashMap[str1], hashMap[str2])); 68 } else { 69 ans.push_back(-1.0); 70 } 71 } 72 return ans; 73 } 74 };
标签:parent,weight,int,查集,values,queries,求值,399,equations 来源: https://www.cnblogs.com/MGFangel/p/16208434.html