ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

399. 除法求值(并查集)

2022-04-29 22:04:31  阅读:205  来源: 互联网

标签:parent weight int 查集 values queries 求值 399 equations


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有