leecode 399. 除法求值

leecode 399. 除法求值

题目描述:

给出方程式 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的情况,且不存在任何矛盾的结果。

解题思路:

1)此题相当于找起始点到终点路径,(判断start,end是否联通);
2)map<string, int> content 将string 和 int 建立映射,确定string的种类数;
3)利用FloyD算法计算start->end的值即可;
4)找到对应的FloyD[map[start]][map[end]]值;即为所求的结果 ;

class Solution {
public:
    vector<double> calcEquation(vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries) {
        map<string , int> content ;
        vector<double> retDouble ;
        int c_i = 0 , c_j , c_k ;
        for (auto equ : equations)
        {
            if (content.find(equ.first) == content.end())
                content[equ.first] = c_i ++ ;
            if (content.find(equ.second) == content.end())
                content[equ.second] = c_i ++ ;
        }
        int lenMap = c_i ;
        vector<vector<double>> Floyed(c_i , vector<double>(c_i , 0)) ;
        
        c_i = 0 ;
        for (auto equ : equations)
        {
            Floyed[content[equ.first]][content[equ.second]] = values[c_i] ;
            Floyed[content[equ.second]][content[equ.first]] = 1.0 / values[c_i] ;
            Floyed[content[equ.first]][content[equ.first]] = 1 ;
            Floyed[content[equ.second]][content[equ.second]] = 1 ;
            c_i ++ ;
        }
        
        for(c_k = 0 ; c_k < lenMap ; c_k ++)
            for(c_i = 0 ; c_i < lenMap ; c_i ++)
                for(c_j = 0 ; c_j < lenMap ; c_j ++)
                {
                    if(Floyed[c_i][c_k] && Floyed[c_k][c_j] && Floyed[c_i][c_j] < Floyed[c_i][c_k] * Floyed[c_k][c_j])
                    {
                        Floyed[c_i][c_j] = Floyed[c_i][c_k] * Floyed[c_k][c_j];
                        Floyed[c_j][c_i] = 1.0 / Floyed[c_i][c_j] ;
                    }
                }
        
        string first , second ;
        double tmpD ;
        for (auto answer : queries)
        {
            first = answer.first ;
            second = answer.second ;
            if (content.find(first) == content.end() || content.find(second) == content.end())
                retDouble.push_back(-1.0) ;
            else if (first == second)
                retDouble.push_back(1.0) ;
            else
            {
                tmpD = Floyed[content[first]][content[second]] ;
                if (tmpD == 0)
                    retDouble.push_back(-1.0) ;
                else
                    retDouble.push_back(tmpD) ;

            }
        }
        return retDouble ;
    }
};

猜你喜欢

转载自blog.csdn.net/u010155337/article/details/88648494
今日推荐