【第三周】399. Evaluate Division

来源:互联网 发布:阿里云提供的服务 编辑:程序博客网 时间:2024/06/17 03:07

原题:

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.

leetcode地址:https://leetcode.com/problems/evaluate-division/description/

解题思路:

给出一系列除法算式,求任意两个代数的商。

可以用图作为数据结构模型,来建立各代数之间的关系:
将每一个代数作为一个顶点,每一个除法式子便成为连接两点的一条无向边,这样将所有的条件全部转化为图的顶点与边的信息后,便构成了一个无向图。

求两个代数的商,便转化成了求图中两个顶点的连通性问题,可以使用dfs算法解决。

在顶点与边的数据结构的设计过程中,每一个顶点需要保存所连通的其它各顶点信息,同时也需要保存每一条边所对应的算式的商。

当给出一个代数时,为了快速定位到图中顶点,可以使用hash。

代码

class Solution {public:    vector<double> calcEquation(vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries) {        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) {    //图中无此顶点则新建顶点                Node* p1 = new Node();                p1->name = s1;                Node* p2 = new Node();                p2->name = s2;                p1->connected.push_back(p2);    //保存顶点信息                p1->values.push_back(values[i]);                p2->connected.push_back(p1);                p2->values.push_back(1 / values[i]);                                map.insert(pair<string, Node*>(s1, p1));    //用hash存储顶点                map.insert(pair<string, Node*>(s2, p2));            } else if (map.count(s1) == 0) {                Node* p1 = new Node();                p1->name = s1;                Node* p2 = map[s2];                p1->connected.push_back(p2);                p1->values.push_back(values[i]);                p2->connected.push_back(p1);                p2->values.push_back(1 / values[i]);                map.insert(pair<string, Node*>(s1, p1));            } else if (map.count(s2) == 0) {                Node* p2 = new Node();                p2->name = s2;                Node* p1 = map[s1];                p1->connected.push_back(p2);                p1->values.push_back(values[i]);                p2->connected.push_back(p1);                p2->values.push_back(1 / values[i]);                map.insert(pair<string, Node*>(s2, p2));            } else {                Node* p1 = map[s1];                Node* p2 = map[s2];                               p1->connected.push_back(p2);                p1->values.push_back(values[i]);                p2->connected.push_back(p1);                p2->values.push_back(1 / values[i]);            }        }        for (int i = 0; i < queries.size(); i++) {            visited.clear();    //每次求商前全部顶点还原成未访问状态            string s1 = queries[i].first, s2 = queries[i].second;            res.push_back(query(s1, s2));               }        return res;    }private:    struct Node {        string name;        vector<Node*> connected;    //相邻顶点        vector<double> values;    //相邻顶点对应的边保存的商    };    unordered_set<string> visited;    //dfs时使用的访问情况表    double query(string s1, string s2) {        if (!map.count(s1) || !map.count(s2)) return -1;        if (s1 == s2) return 1;        return dfs(map[s1], map[s2]);    }    double dfs(Node* p, Node* target) {        visited.insert(p->name);    //标记为已访问        for (int i = 0; i < p->connected.size(); i++) {    //找到目标顶点则返回            if (p->connected[i]->name == target->name) return p->values[i];        }        for (int i = 0; i < p->connected.size(); i++) {            if (visited.count(p->connected[i]->name) == 0) {                double temp = dfs(p->connected[i], target);    //dfs                if (temp != -1) return p->values[i] * temp;            }        }        return -1;    }};

总结

  1. 如何构建数据结构是题目的关键,在用图存储信息时,要想清楚每个顶点所需要保存的内容,以及边上需要保留的信息。在建立完图之后,剩余的搜索工作便能够很轻松的解决了。

  2. 使用hash。灵活的使用map、set等结构,代码会写的更轻松,同时复杂度也会减小。

原创粉丝点击