399. Evaluate Division

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<double>& values, vector<pair<string, string>> queries , where equations.size() == values.size(), and the values are positive. This represents the equations. Return vector<double>.

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 class Solution {
 2 public:
 3     vector<double> calcEquation(vector<pair<string, string>> &equations, vector<double>& values, vector<pair<string, string>> &queries) {
 4         unordered_map<string,int>vertices = getVertices(equations);
 5         vector<vector<double>> graph = buildGraph(vertices, equations, values);
 6         
 7         vector<double> result(queries.size());
 8         vector<bool> visited(vertices.size());
 9         for(int i=0;i<queries.size();i++){
10             string from = queries[i].first;
11             string to = queries[i].second;
12             if(vertices.find(from)==vertices.end()||vertices.find(to)==vertices.end()){
13                 result[i]=-1;
14             }else{
15                 fill(visited.begin(),visited.end(),false);
16                 result[i]=dfs(vertices[from], vertices[to], graph,  visited);
17             }
18         }
19         
20         return result;
21     }
22 private:
23     //Mapping from string to integer
24     unordered_map<string,int> getVertices(vector<pair<string,string>>& equations){
25         unordered_map<string,int> map;
26         int i = 0;
27         for(auto equation: equations){
28             if(map.find(equation.first)==map.end()){
29                 map[equation.first] = i;
30                 i++;
31             }
32             if(map.find(equation.second)==map.end()){
33                 map[equation.second] = i;
34                 i++;
35             }
36         }
37         return map;
38     }
39     
40     vector<vector<double>> buildGraph(unordered_map<string,int>& vertices, vector<pair<string,string>>& equations, vector<double>& values){
41         vector<vector<double>> graph (vertices.size(),vector<double>(vertices.size()));
42         for(int i=0;i<vertices.size();i++){
43             graph[i][i] = 1.0;
44         }
45         for(int i=0;i<equations.size();i++){
46             int a = vertices[equations[i].first];
47             int b = vertices[equations[i].second];
48             graph[a][b] = values[i];
49             graph[b][a] = 1.0/values[i];
50         }
51         
52         return graph;
53     }
54     
55     double dfs(int from, int to, vector<vector<double>>& graph, vector<bool>& visited){
56         for(int j=0; j<graph[from].size();j++){
57             if(graph[from][j]!=0&&!visited[j]){
58                 if(j==to){
59                     return graph[from][to];
60                 }else{
61                     visited[j] = true;
62                     double result = dfs(j,to,graph,visited);
63                     if(result!=-1){
64                         return graph[from][j]*result;   
65                     }
66                     visited[j]=false;
67                 }
68             }
69         }
70         
71         return -1;
72     }
73 };

 

转载于:https://www.cnblogs.com/ruisha/p/9222504.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值