Leetcode C++《热题 Hot 100-49》399.除法求值

Leetcode C++《热题 Hot 100-49》399.除法求值

  1. 题目

给出方程式 A / B = k, 其中 A 和 B 均为代表字符串的变量, k 是一个浮点型数字。根据已知方程式求解问题,并返回计算结果。如果结果不存在,则返回 -1.0。

示例 :
给定 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 ]

输入为: vector<pair<string, string>> equations, vector& values, vector<pair<string, string>> queries(方程式,方程式结果,问题方程式), 其中 equations.size() == values.size(),即方程式的长度与方程式结果长度相等(程式与结果一一对应),并且结果值均为正数。以上为方程式的描述。 返回vector类型。

基于上述例子,输入如下:

equations(方程式) = [ [“a”, “b”], [“b”, “c”] ],
values(方程式结果) = [2.0, 3.0],
queries(问题方程式) = [ [“a”, “c”], [“b”, “a”], [“a”, “e”], [“a”, “a”], [“x”, “x”] ].
输入总是有效的。你可以假设除法运算中不会出现除数为0的情况,且不存在任何矛盾的结果。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/evaluate-division
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

  1. 思路
  • 题目提示使用并查集的方法,一个新的概念,之后可以一系列的做一堆题目学习并查集
  • 我的方案:使用bfs遍历,空间换取时间,如果a/b能够通过a进行bfs遍历到b,在这个过程中不断更新乘积。比如a/c 【从节点a到节点c】* c/b【从节点c到节点b】 = a/b
  • 时间复杂度:查询一次,最多o(n)因为是bfs遍历,最多遍历所有的节点
  • 空间复杂度:主要存储这棵树,一个节点的所有孩子,以及和孩子之间的权值,n^ 2的复杂度
  1. 代码
class Solution {
public:
    map<string, vector<pair<string, double>>> relations;
    vector<double> calcEquation(vector<vector<string>>& equations, vector<double>& values, vector<vector<string>>& queries) {
        //思路不是很好,比如说a = 1, 那b= 0.5, 然后根据其他的式子得到c=0.5/3 [有小数点有误差]
        //提示说的是并查集,看了题解发现BFS的思路很是神通,边是关系a-b的大小是2,b到a是0.5,根据边的路径更新
        for (int i = 0 ; i < values.size(); i++) {
            string a = equations[i][0];
            string b = equations[i][1];
            double abVal = values[i];
            pair one = make_pair(b, abVal);
            pair two = make_pair(a, 1.0/abVal);
            map<string, vector<string>>::iterator it;
            if(relations.find(a) == relations.end()) {
                vector<pair<string, double>> temp;
                temp.push_back(one);
                relations.insert(make_pair(a,temp));
            } else {
                relations[a].push_back(one);
            }
            if(relations.find(b) == relations.end()) {
                vector<pair<string, double>> temp;
                temp.push_back(two);
                relations.insert(make_pair(b,temp));
            } else {
                relations[b].push_back(two);
            }
        }
        vector<double> res;
        for (int i = 0; i < queries.size(); i++)
            res.push_back(getRes(queries[i][0], queries[i][1]));
        return res;
    }

    double getRes(string x, string y) {
        if (relations.find(x) == relations.end())
            return -1;
        if (relations.find(y) == relations.end())
            return -1;
        queue<pair<string, double>> bfsNode;
        map<string, bool> existBfs;
        bfsNode.push(make_pair(x, 1.0));
        while(!bfsNode.empty()) {
            pair<string, double> one = bfsNode.front();
            bfsNode.pop();
            vector<pair<string, double>> rels = relations[one.first];
            for (int i = 0 ; i < rels.size(); i++) {
                if (existBfs.find(rels[i].first) == existBfs.end()) {
                    if (y == rels[i].first) {
                        return one.second*rels[i].second;
                    }
                    existBfs.insert(make_pair(rels[i].first, true));
                    bfsNode.push(make_pair(rels[i].first, one.second*rels[i].second));
                }
            }
        }
        return -1.0;
    }
};
发布了205 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Alexia23/article/details/104913196
今日推荐