Leetcode刷题笔记 399. 除法求值

399. 除法求值

时间:2021年1月6日
知识点:图、广度优先搜索、Floyd、并查集
题目链接

题目
给你一个变量对数组 equations 和一个实数值数组 values 作为已知条件,其中 equations[i] = [Ai, Bi] 和 values[i] 共同表示等式 Ai / Bi = values[i] 。每个 Ai 或 Bi 是一个表示单个变量的字符串。

另有一些以数组 queries 表示的问题,其中 queries[j] = [Cj, Dj] 表示第 j 个问题,请你根据已知条件找出 Cj / Dj = ? 的结果作为答案。

返回 所有问题的答案 。如果存在某个无法确定的答案,则用 -1.0 替代这个答案。

注意:输入总是有效的。你可以假设除法运算中不会出现除数为 0 的情况,且不存在任何矛盾的结果。

示例 1
输入:equations = [[“a”,“b”],[“b”,“c”]], values = [2.0,3.0], queries = [[“a”,“c”],[“b”,“a”],[“a”,“e”],[“a”,“a”],[“x”,“x”]]
输出:[6.00000,0.50000,-1.00000,1.00000,-1.00000]
解释
条件: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 ]

示例 2
输入:equations = [[“a”,“b”],[“b”,“c”],[“bc”,“cd”]], values = [1.5,2.5,5.0], queries = [[“a”,“c”],[“c”,“b”],[“bc”,“cd”],[“cd”,“bc”]]
输出:[3.75000,0.40000,5.00000,0.20000]

示例 3
输入:equations = [[“a”,“b”]], values = [0.5], queries = [[“a”,“b”],[“b”,“a”],[“a”,“c”],[“x”,“y”]]
输出:[0.50000,2.00000,-1.00000,-1.00000]

提示:

  • 1 <= equations.length <= 20
  • equations[i].length == 2
  • 1 <= Ai.length, Bi.length <= 5
  • values.length == equations.length
  • 0.0 < values[i] <= 20.0
  • 1 <= queries.length <= 20
  • queries[i].length == 2
  • 1 <= Cj.length, Dj.length <= 5
  • Ai, Bi, Cj, Dj 由小写英文字母与数字组成

解法

  1. 首先需要把字符串 对应成数字 方便后面计算 用哈希表
  2. 用稀疏表 构建图 每个数字连接的数字 构成边 确定边的权重
  3. 用广度优先算法 迪杰斯特拉算法 从第一个数找到最后要求的数 边走边更新
  4. 如果查询很多 可以用floyd先构建完整的图 而不是走一步算一步
  5. 并查集的方法还没看

代码

//头文件
#include "cheader.h"
/*
class Solution {
public:
    vector<double> calcEquation(vector<vector<string>>& equations, vector<double>& values, vector<vector<string>>& queries) {
        int num = 0;
        unordered_map<string, int> str_num;
        for(vector<string> v:equations){
            if(str_num.find(v[0]) == str_num.end())
                str_num[v[0]] = num++;
            if(str_num.find(v[1]) == str_num.end())
                str_num[v[1]] = num++;
        }
        
        int n = equations.size();
        vector<vector<pair<int, double>>> edge(num);
        for(int i = 0;i < n;i++){
            int a = str_num[equations[i][0]];
            int b = str_num[equations[i][1]];
            edge[a].push_back({b,values[i]});
            edge[b].push_back({a,1.0/values[i]});
        }
        
        vector<double> ans;
        for(vector<string> q:queries){
            double ret = -1.0;
            if(str_num.find(q[0]) != str_num.end() && str_num.find(q[1]) != str_num.end()){
                int a = str_num[q[0]];
                int b = str_num[q[1]];
                if(a == b)
                    ret = 1.0;
                else{
                    queue<double> q;
                    q.push(a);
                    vector<double> dist(num,-1.0);
                    dist[a] = 1;
                    while(!q.empty()){
                        if(dist[b] != -1.0)
                            break;
                        int t = q.front();
                        q.pop();
                        for(pair<int, double> bian:edge[t]){
                            if(dist[bian.first] == -1){
                                dist[bian.first] = bian.second * dist[t];
                                q.push(bian.first);
                            }
                        }
                    }
                    ret = dist[b];
                }
            }
            ans.push_back(ret);
        }
        return ans;
    }
};
*/
class Solution {
public:
    vector<double> calcEquation(vector<vector<string>>& equations, vector<double>& values, vector<vector<string>>& queries) {
        int num = 0;
        unordered_map<string, int> str_num;
        for(vector<string> v:equations){
            if(str_num.find(v[0]) == str_num.end())
                str_num[v[0]] = num++;
            if(str_num.find(v[1]) == str_num.end())
                str_num[v[1]] = num++;
        }
        
        int n = equations.size();
        vector<vector<double>> graph(num, vector<double>(num, -1.0));
        for (int i = 0; i < n; i++) {
            int a = str_num[equations[i][0]];
            int b = str_num[equations[i][1]];
            graph[a][b] = values[i];
            graph[b][a] = 1.0 / values[i];
        }
        
        for(int k = 0; k < num; k++)
            for(int i = 0; i < num; i++)
                for(int j = 0; j < num; j++)
                    if(graph[i][k] > 0 && graph[k][j] > 0)
                        graph[i][j] = graph[i][k] * graph[k][j];
        
        vector<double> ans;
        for (vector<string> q:queries){
            double ret = -1.0;
            if(str_num.find(q[0]) != str_num.end() && str_num.find(q[1]) != str_num.end()){
                int a = str_num[q[0]];
                int b = str_num[q[1]];
                if (graph[a][b] > 0){
                    ret = graph[a][b];
                }
            }
            ans.push_back(ret);
        }
        return ans;
    }
};

int main()
{
    vector<vector<string>> equations;
    vector<double> values;
    vector<vector<string>> queries;
    equations.push_back({"a","b"});
    equations.push_back({"b","c"});
    values.push_back(2.0);
    values.push_back(3.0);
    queries.push_back({"a","c"});
    queries.push_back({"b","a"});
    queries.push_back({"a","e"});
    queries.push_back({"a","a"});
    queries.push_back({"x","x"});
    Solution s;
    vector<double> ans = s.calcEquation(equations, values, queries);
    for(double x:ans)
        cout<<x<<endl;
    return 0;
}


今天也是爱zz的一天哦!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值