Algorithms(三)Evaluate Division
来源:互联网 发布:网络销售岗位职责 编辑:程序博客网 时间:2024/05/18 02:35
题目:
Evaluate Division
Equations are given in the format A / B = k
, where A
andB
are variables represented as strings, andk
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 ].
The input is: vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries
, whereequations.size() == values.size()
, and the values are positive. This represents the equations. Returnvector<double>
.
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.
代码:
class Solution {public: vector<double> calcEquation(vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries) { // 注释1 unordered_map<string, Node*> map; // 注释2 vector<double> res; for (int i = 0; i < equations.size(); i++) { string s1 = equations[i].first, s2 = equations[i].second; if (map.count(s1) == 0 && map.count(s2) == 0) { map[s1] = new Node(); map[s2] = new Node(); map[s1] -> value = values[i]; map[s2] -> value = 1; map[s1] -> parent = map[s2]; } else if (map.count(s1) == 0) { map[s1] = new Node(); map[s1] -> value = map[s2] -> value * values[i]; map[s1] -> parent = map[s2]; } else if (map.count(s2) == 0) { map[s2] = new Node(); map[s2] -> value = map[s1] -> value / values[i]; map[s2] -> parent = map[s1]; } else { unionNodes(map[s1], map[s2], values[i], map); } } for (auto query : queries) { if (map.count(query.first) == 0 || map.count(query.second) == 0 || findParent(map[query.first]) != findParent(map[query.second])) res.push_back(-1); else res.push_back(map[query.first] -> value / map[query.second] -> value); } return res; } private: struct Node { Node* parent; double value = 0.0; Node() {parent = this;} }; void unionNodes(Node* node1, Node* node2, double num, unordered_map<string, Node*>& map) { Node* parent1 = findParent(node1), *parent2 = findParent(node2); double ratio = node2 -> value * num / node1 -> value; for (auto it = map.begin(); it != map.end(); it++) { // 注释3 if (findParent(it -> second) == parent1) { it -> second -> value *= ratio; } } parent1 -> parent = parent2; } Node* findParent(Node* node) { if (node -> parent == node) return node; node -> parent = findParent(node -> parent); return node -> parent; }};
算法:
1. 遍历equations的每一项,第i个等式满足 s1/s2 = values[i],分四种情况讨论:
· 若图中没有s1,s2,则建立两个新节点,令s2 = 1,s1 = values[i]。因为s1是以s2的值为基准取值的,所以令s1的父节点为s2。
· 若图中没有s1,有s2,则建立新节点s1,令s1 = s2 * values[i]。因为s1是以s2的值为基准取值的,所以令s1的父节点为s2。
· 若图中有s1,没有s2,则建立新节点s2,令s2 = s1 / values[i]。因为此时,s2是以s1的值为基准进行取值的,所以令s2的父节点为s1。
· 若图中已经有了s1,s2,则从s1的父节点不断递归,找到s1的根节点parent1,同理,找到s2的根节点。然后将图中所有根节点为parent1的点(包括s1的根节点本身),都乘以参数ratio,该参数满足s1 * ratio / s2 = values[i]。因为此时,parent1是以parent2为基准取值的,所以令parent1的父节点为parent2。
2. 遍历queries的每一项,分两种情况处理:
· 若queries中的两个量s1、s2没有同时存在与图中,或者这两个量的根节点不同,无法比较,则返回-1.
· 若s1/s2的值存在,则返回该值。
注释:
注释1:c++ 中 pair 的 使用方法
注释2:C++ map的基本操作和使用 ; Map集合的四种遍历方式
注释3:C++11特性:auto关键字;C++ iterator->second意思
- Algorithms(三)Evaluate Division
- LeetCode-algorithms 399. Evaluate Division
- Evaluate Division
- Evaluate Division
- Evaluate Division
- Evaluate Division
- Evaluate Division
- Evaluate Division
- Evaluate Division
- Evaluate Division
- Evaluate Division
- Evaluate Division
- Evaluate Division
- LeetCode 399 Evaluate Division(BFS)
- 【Leetcode】399. Evaluate Division
- [leetcode]399. Evaluate Division
- 399. Evaluate Division
- leetcode:399. Evaluate Division
- Hello Bolg
- icml和nips等各类重要会议论文收集
- 基于servlet的登录验证
- 对象与内存控制
- radio post只传第一个radio的值,用JQuery的ajax提交radio选项
- Algorithms(三)Evaluate Division
- java中的内部类详解
- Java核心技术
- 升级caffe所对应cudnn到v5以上版本
- HDU DNA Sorting (树状数组求逆序对)
- Vue之监听数据变化
- Document/View是MFC的精髓?
- ItemCF与UserCF协同过滤算法简单入门和一般过程
- 文章标题 实验二 命令行菜单小程序V1.0