利用哈希表和dfs解决LeetCode 399. Evaluate Division

问题简介

给定一些由变量组成的等式组,然后根据这些等式推算出所闻的等式的结果,如果无法推算,则返回-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 ]

注:给定的等式组不存在结果为0的情况。

解题思路

先构造一个数据结构,然后根据给定的等式构造一个map,便于访问,这其实就相当与是一幅稀疏图。
要注意的是,这里在保存 a/b=2.0 这样的等式时,同时也保存了 b/a=0.5

// 构造一个结构体
struct 节点{
    是否访问过;
    unordered_map<除数, 结果>;
};

// 构造稀疏图
unordered_map<变量, 节点>;

然后根据询问的内容做深度优先遍历即可。

源代码

struct Node{
    bool visited;
    unordered_map<string, double> children;
    Node(){
        visited = false;
    }
};

class Solution {
private:
    vector<double> res;

private:
    bool dfs(unordered_map<string, Node*>& mp, pair<string, string>& p, double r){
        if (mp.find(p.first) != mp.end() && !mp[p.first]->visited){
            mp[p.first]->visited = true;
            if (mp[p.first]->children.find(p.second) != mp[p.first]->children.end()){
                res.push_back(r * mp[p.first]->children[p.second]);
                mp[p.first]->visited = false;
                return true;
            }
            else{
                for (auto iter = mp[p.first]->children.begin(); iter != mp[p.first]->children.end(); iter++){
                    pair<string, string> new_p = make_pair(iter->first, p.second);
                    if (dfs(mp, new_p, r * iter->second)){
                        mp[p.first]->visited = false;
                        return true;
                    }
                }
            }
            mp[p.first]->visited = false;
        }
        return false;
    }

public:
    vector<double> calcEquation(vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries) {
        // 构造图
        unordered_map<string, Node*> mp;
        for (int i = 0; i < equations.size(); i++){
            // 保存a/b
            if (mp.find(equations[i].first) == mp.end()){
                mp[equations[i].first] = new Node();
            }
            Node* tmp = mp[equations[i].first];
            if (tmp->children.find(equations[i].second) == tmp->children.end()){
                tmp->children[equations[i].second] = values[i];
            }

            // 保存b/a
            if (mp.find(equations[i].second) == mp.end()){
                mp[equations[i].second] = new Node();
            }
            tmp = mp[equations[i].second];
            if (tmp->children.find(equations[i].first) == tmp->children.end()){
                tmp->children[equations[i].first] = 1.0 / values[i];
            }
        }

        // 开始执行query
        for (int i = 0; i < queries.size(); i++){
            if (queries[i].first == queries[i].second){
                if (mp.find(queries[i].first) != mp.end())
                    res.push_back(1.0);
                else
                    res.push_back(-1.0);
            }
            else{
                if (!dfs(mp, queries[i], 1.0)){
                    res.push_back(-1.0);
                }
            }
        }

        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七元权

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值