题目:Evaluate Division
原题链接:https://leetcode.com/problems/evaluate-division/
Equations are given in the format A / B = k, where A and B are variables represented as strings, and k is a real number (floating point number). Given some queries, return the answers. If the answer does not exist, return -1.0.
Example:
Given a / b = 2.0, b / c = 3.0.
queries are: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ? .
return [6.0, 0.5, -1.0, 1.0, -1.0 ].
According to the example above:
equations = [ [“a”, “b”], [“b”, “c”] ],
values = [2.0, 3.0],
queries = [ [“a”, “c”], [“b”, “a”], [“a”, “e”], [“a”, “a”], [“x”, “x”] ].
The input is always valid. You may assume that evaluating the queries will result in no division by zero and there is no contradiction.
给出一系列字符串之间的比率关系,求任意两个字符串之间的比率。
首先要考虑到字符串是不是处于同一个块,可以用并查集存储它们的关系,其次,当扫描到的两个字符串都已经出现过的时候,意味着可能原先两个不相连的块通过它们连接了起来,这就需要调整当中一块的所有元素的大小。
用两个map,第一个mp用来来将字符串隐射到double值,第二个father用来把字符串映射到字符串,用来存储联通块的联通信息。
遍历字符串pair数组,可能出现如下三种情况(设被除数字符串为s1,除数字符串为s2,比率为 n ):
情况一: s1和s2在mp中都没有出现过,这种情况直接让第二个字符串的mp映射为1,第一个字符串的mp映射为n,然后让father[ s2 ] = s1, father[ s1 ] = s1即可。
情况二: s1和s2中只有一个没有出现过,让这个字符串的mp根据除法关系映射为相应的double值,然后父节点指向另外一个即可。
情况三: s1和s2都出现过,由于题目保证数据可靠不会冲突,这就说明当前两个字符串在之前是属于两个不同的联通块,但是此时说明他们是相连的,由于这两个联通块建立的时候都是以1为基底的,这个时候就需要把当中一个联通块里面的元素的double值按照这两个的倍率关系调整一下。假设调整s1所在的联通块,那么调整的倍率是mp[ s2 ] * n / mp[ s1 ]。然后遍历mp,把所有属于这个联通块的元素的double值都乘前面的数。
最后根据要查询的字符串pair返回相应的值即可。
代码如下:
class Solution {
private:
map<string, string> father;
public:
vector<double> calcEquation(vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries) {
map<string, double> mp;
vector<double> ans;
int len = equations.size();
for(int i = 0; i < len; ++i) {
if(mp.count(equations[i].first) == 0 && mp.count(equations[i].second) == 0) {
mp[equations[i].second] = 1;
mp[equations[i].first] = values[i];
father[equations[i].first] = equations[i].first;
father[equations[i].second] = equations[i].first;
}else if(mp.count(equations[i].first) == 0) {
mp[equations[i].first] = values[i] * mp[equations[i].second];
father[equations[i].first] = equations[i].second;
}else if(mp.count(equations[i].second) == 0) {
mp[equations[i].second] = mp[equations[i].first] / values[i];
father[equations[i].second] = equations[i].first;
}else unionFather(equations[i].first, equations[i].second, values[i], mp);
}
len = queries.size();
for(int i = 0; i < len; ++i) {
if(mp.count(queries[i].first) == 0 || mp.count(queries[i].second) == 0 || findFather(queries[i].first) != findFather(queries[i].second)) ans.push_back(-1);
else ans.push_back(mp[queries[i].first] / mp[queries[i].second]);
}
return ans;
}
string findFather(string s) {
while(father[s] != s) s = father[s];
return s;
}
void unionFather(string s1, string s2, double r, map<string, double>& mp) {
string f1 = findFather(s1), f2 = findFather(s2);
double ra = mp[s2] * r / mp[s1];
for(auto it = mp.begin(); it != mp.end(); ++it) {
if(findFather(it->first) == f1) {
mp[it->first] *= ra;
}
}
father[f2] = f1;
}
};
本文介绍了一种解决LeetCode上Evaluate Division问题的方法。该问题要求根据给定的变量比率计算查询对之间的比率。通过使用并查集和两个map数据结构,文章详细阐述了如何有效地处理并解决这一挑战。
512

被折叠的 条评论
为什么被折叠?



