• 399. Evaluate Division--Medium


    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 ].

    The input is: vector<pair<string, string>> equations, vector& values, vector<pair<string, string>> queries , where equations.size() == values.size(), and the values are positive. This represents the equations. Return vector.

    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.

    1.思考:最开始想到的是用Graph的方法:

    • 构建一个图,即有关系的变量之间相当于是双向连通的,但系数不相同。eg. a/b=2.0,a-b权值为2.0,b-a权值为1/2.0=0.5;
    • 对于queries中的每一个被除数和除数,利用DFS进行搜索,从而求解;
    • 在dfs函数中,若从被除数到除数找到一条路径,则在子函数中将结果加入到res中;若未找到,则子函数什么都不做,并通过在主函数中判断res的size来确定dfs是否找到,若未找到则将-1加入res中;
    • 用unordered_map占用的内存会比较大,还要多思考其他方案。

    2.知识点:
    (1) unordered_map<string, string> m

    • 插入有两种方式:

      (a) m["a"] = "b";

      (b) m.insert(make_pair("a", "b"));
    • 取数据有两种方式:

      (a) 取出"a"之后的内容,即“b”,m["a"];

      (b) 取出“a”、“b”,m[0].first,m[0].second;
    • 查找是否存在某元素有两种方式,以不能找到为例:

      (a) m.count("a")==0 ;

      (b) m.find("a") == m.end();

      (2) unordered_map<string, unordered_map<string, double>> m
    • 二重unordered_map的使用方法和一重的基本一致,就是使用过程中要小心注意。

    3.实现

    • Runtime: 4ms (100%)
    • Memory: 9.5MB (60.58%)
    class Solution {
    public:
        //DFS
        unordered_map<string, unordered_map<string, double>> m;
        vector<double> res;
        
        vector<double> calcEquation(vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries) {
            //Construct Map
            //eg. m[b]: ['a', 0.5], ['c', 3.0]
            int len = equations.size();
            unordered_map<string, double> pair;
            for(int i=0; i<len; i++){
                auto eq = equations[i];
                auto eq0 = eq.first, eq1 = eq.second; 
                m[eq0].insert(make_pair(eq1, 1.0*values[i]));
                m[eq1].insert(make_pair(eq0, 1.0/values[i]));
            }
            
            unordered_map<string, double> d;
            vector<string> visit;
            double ans;
            for(int i=0; i<queries.size(); i++){
                auto q = queries[i];
                visit.clear();
                dfs(q.first, q.second, visit, 1.0);
                if(res.size()!=i+1)
                    res.push_back(-1.0);
            }
            
            return res;
        }
        
        void dfs(string a, string b, vector<string>& visit, double ans)
        {
            if(m.count(a)!=0 && a==b){            
                res.push_back(1.0);
                return;
            }
            
            visit.push_back(a);
            for(auto mp:m[a]){
                if(find(visit.begin(), visit.end(), mp.first)==visit.end()){//can't find it 
                    if(mp.first==b){
                        visit.push_back(b);
                        ans *= mp.second;
                        res.push_back(ans);
                        return;
                    }
                    else
                        dfs(mp.first, b, visit, ans*mp.second);
                }   
            }
        }    
        
    };
    
    
  • 相关阅读:
    Linq的一些常见Demo
    有一名员工发现日历已经7天没有翻了,于是他连着翻了7页,7天的总和刚好是138,问这一天是几号?
    20块钱,1块钱1瓶,两个空瓶子可以换一瓶,问最多可以喝几瓶?
    【转】Java编程之字符集问题研究
    Reset / Validate Buffer
    Article Master Data Deviation
    STAD Parameters
    Linux11.2 MySQL常用命令
    Linux11.1 设置更改Mysql的root密码及连接mysql
    Linux5.10 告警系统
  • 原文地址:https://www.cnblogs.com/xuyy-isee/p/10539527.html
Copyright © 2020-2023  润新知