I'm posting my C++ code for LeetCode's Evaluate Division. If you have time and would like to review, please do so. Thank you!
Problem
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 ].
The input is: vector<pair<string, string>> equations, vector& values, vector<pair<string, string>> queries , where equations.size() == values.size(), and the values are positive. This represents the equations. Return vector.
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.
Accepted C++
class Solution { public: std::vector<double> calcEquation(std::vector<vector<string>> equations, std::vector<double> values, std::vector<vector<string>> const& queries) { generate_graph(equations, values); std::vector<double> evaluates; for (auto& query : queries) { std::set<string> visited; double accumulated = 1.0; bool evaluate = solve_equation(query[0], query[1], accumulated, visited); if (evaluate) { evaluates.push_back(accumulated); } else { evaluates.push_back(-1); } } return evaluates; } protected: struct Node { Node(std::string const parameter, double const evaluate) { this -> parameter = parameter; this -> evaluate = evaluate; } std::string parameter; double evaluate; }; map<string, vector<Node>> graph; void generate_graph(std::vector<vector<string>>& equations, std::vector<double>& values) { for (auto const& equation : equations) { vector<Node> children; graph[equation[0]] = children; } for (int equation = 0; equation < equations.size(); equation++) { Node node(equations[equation][1], values[equation]); graph[equations[equation][0]].push_back(node); Node node_reverse(equations[equation][0], 1 / values[equation]); graph[equations[equation][1]].push_back(node_reverse); } } bool solve_equation(std::string const nominator, std::string const denominator, double& accumulated, std::set<string>& visited) { if (nominator == denominator && graph.find(nominator) != graph.end()) { return true; } if (graph.find(nominator) == graph.end() || graph.find(denominator) == graph.end()) { return false; } visited.insert(nominator); for (auto& child : graph[nominator]) { if (visited.find(child.parameter) == visited.end()) { accumulated *= child.evaluate; if (child.parameter == denominator) { return true; } if (solve_equation(child.parameter, denominator, accumulated, visited)) { return true; } else { accumulated /= child.evaluate; } } } return false; } };
Reference
On LeetCode, there is an instance usually named Solution
with one or more public
functions which we are not allowed to rename those.
node_reverse
, would the bug have been "obvious"?)\$\endgroup\$