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.
写了逻辑错误的代码,下次 别犯这种错误:
import java.util.HashMap;
//这个逻辑不是对的 要用搜索的方式
public class Solution {
public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
HashMap<String,Integer> map=new HashMap<String,Integer>();
int index=0;
for(int i=0;i<equations.length;i++){
if(!map.containsKey(equations[i][0])) map.put(equations[i][0],index++);
if(!map.containsKey(equations[i][1])) map.put(equations[i][1],index++);
}
double[][] graph=new double[index][index];
for(int i=0;i<graph.length;i++){
for(int j=0;j<graph[0].length;j++){
if(i!=j)graph[i][j]=-1.0;
else graph[i][j]=1;
}
}
for(int i=0;i<equations.length;i++){
int x=map.get(equations[i][0]);
int y=map.get(equations[i][1]);
graph[x][y]=values[i];
graph[y][x]=1/values[i];
for(int j=0;j<graph[0].length;j++){
if(graph[j][x]!=-1.0) {
graph[j][y]=graph[j][x]*graph[x][y];
graph[y][j]=1/graph[j][y];
}
if(graph[j][y]!=-1.0){
graph[j][x]=graph[j][y]*graph[y][x];
graph[x][j]=1/graph[j][x];
}
}
}
double[] re=new double[queries.length];
for(int i=0;i<queries.length;i++){
Integer x=map.get(queries[i][0]);
Integer y=map.get(queries[i][1]);
if(x==null||y==null) re[i]=-1.0;
else re[i]=graph[x][y];
}
return re;
}
}
ac代码:
public class Solution2 {
public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
HashMap<String,Integer> map=new HashMap<String,Integer>();
int index=0;
for(int i=0;i<equations.length;i++){
if(!map.containsKey(equations[i][0])) map.put(equations[i][0],index++);
if(!map.containsKey(equations[i][1])) map.put(equations[i][1],index++);
}
graph=new double[index][index];
for(int i=0;i<graph.length;i++){
for(int j=0;j<graph[0].length;j++){
if(i!=j)graph[i][j]=-1.0;
else graph[i][j]=1;
}
}
for(int i=0;i<equations.length;i++){
int x=map.get(equations[i][0]);
int y=map.get(equations[i][1]);
graph[x][y]=values[i];
graph[y][x]=1/values[i];
}
double[] re=new double[queries.length];
for(int i=0;i<queries.length;i++){
Integer x=map.get(queries[i][0]);
Integer y=map.get(queries[i][1]);
if(x==null||y==null) re[i]=-1.0;
else{
boolean[] flag=new boolean[graph.length];
flag[x]=true;
re[i]=dfs(x,y,flag);
}
}
return re;
}
double[][] graph;
public double dfs(int s,int t,boolean[] flag){
if(graph[s][t]!=-1.0) return graph[s][t];
double re=-1.0d;
for(int i=0;i<graph.length;i++){
if(flag[i]) continue;
flag[i]=true;
if(graph[s][i]!=-1.0){
double temp=dfs(i,t,flag);
if(temp!=-1.0) re=graph[s][i]*temp;
if(re!=-1.0) return graph[s][t]=re;
}
flag[i]=false;
}
return graph[s][t]=re;
}
}