399. Evaluate Division

Posted ruisha

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 };

 




以上是关于399. Evaluate Division的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 399. Evaluate Division

399. Evaluate Division

Leetcode 399. Evaluate Division

399. Evaluate Division

399. Evaluate Division

399. Evaluate Division