题意理解:连乘的问题,链式元素,广度搜索
两个数字看做一条边,每条边有权重;给定两个节点,求出两个节点之间所有链路权重的乘积。
题目剖析:1.先将有向权重关系利用 Map
2.利用dfs,遍历所有节点,一直遍历,只有首尾节点符合题目要求的,才符合退出条件,给最终计算结果赋值
题目应用:利用map存储nodeStart和relation的映射关系。利用nodeStart作为链路过程的遍历特征;
链路过程存入visited,防止死循环
dfs设定跳出条件,碰到条件即触发退出遍历机制
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> equations, vector& values, vector> 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.
`
package com.jd.jr.nlp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
/\*\*\*
\* 将所有的关系 做成 edge-class \* 将所有的关系和 start-node 存入map \* 将所有的关系和 end-node 存入map \* \* 遍历所有queries,递归查找开始和结束的节点 \* \* \*/
public class Solution399Diy {
HashMap> mEdges \= new HashMap<>();
double\[\] mRes;
class Edge{
String from;
String to;
double value;
public Edge(String from,String to,double value){
this.from\=from;
this.to\=to;
this.value\=value;
}
}
public double dfs(String nodeStart,String nodeTo,List visited){
if(!mEdges.containsKey(nodeStart)){
return \-1.0;
}
if(!mEdges.containsKey(nodeTo)){
return \-1.0;
}
if(nodeStart.equals(nodeTo)){
return 1.0;
}
for(int i=0;i> equations, double\[\] values, List> queries) {
for(int i=0;i edgeFromList=new ArrayList<>();
edgeFromList.add(edge1);
mEdges.put(edge1.from,edgeFromList);
}else {
mEdges.get(equations.get(i).get(0)).add(edge1);
}
Edge edge2=new Edge(equations.get(i).get(1),equations.get(i).get(0),1.0/values\[i\]);
if(!mEdges.containsKey(equations.get(i).get(1))){
List edgeToList=new ArrayList<>();
edgeToList.add(edge2);
mEdges.put(edge2.from,edgeToList);
}else {
mEdges.get(equations.get(i).get(1)).add(edge2);
}
}
mRes\=new double\[queries.size()\];
List visited=new ArrayList<>();
for(int i=0;i> equations =new ArrayList<>({"a", "b"},{"b", "c"});
// List> equations =\[\["a", "b"\],\["b", "c"\]\];
// List equations =Arrays.asList("a", "b","b", "c");
// List> equations =new ArrayList<>();
// List a1= Arrays.asList("a","b");
// List a2=Arrays.asList("b","c");
// equations.add(a1);
// equations.add(a2);
//// equations.add(Arrays.asList({"a","b"});
//
// double\[\] values = new double\[2\];
// values\[0\]=2.0;
// values\[1\]=3.0;
//// queries = \[ \["a", "c"\], \["b", "a"\], \["a", "e"\], \["a", "a"\], \["x", "x"\] \];
// List> queries=new ArrayList<>();
// List b1=Arrays.asList("a","c");
// List b2=Arrays.asList("b","a");
// List b3=Arrays.asList("a","e");
// List b4=Arrays.asList("a","a");
// List b5=Arrays.asList("x","x");
// queries.add(b1);
// queries.add(b2);
// queries.add(b3);
// queries.add(b4);
// queries.add(b5);
// \[\["x1","x2"\],\["x2","x3"\],\["x3","x4"\],\["x4","x5"\]\]
//\[3.0,4.0,5.0,6.0\]
//\[\["x1","x5"\],\["x5","x2"\],\["x2","x4"\],\["x2","x2"\],\["x2","x9"\],\["x9","x9"\]\]
List> equations =new ArrayList<>();
List a1= Arrays.asList("x1","x2");
List a2=Arrays.asList("x2","x3");
List a3=Arrays.asList("x3","x4");
List a4=Arrays.asList("x4","x5");
equations.add(a1);
equations.add(a2);
equations.add(a3);
equations.add(a4);
// equations.add(Arrays.asList({"a","b"});
double\[\] values = new double\[4\];
values\[0\]=3.0;
values\[1\]=4.0;
values\[2\]=5.0;
values\[3\]=6.0;
// queries = \[ \["a", "c"\], \["b", "a"\], \["a", "e"\], \["a", "a"\], \["x", "x"\] \];
List> queries=new ArrayList<>();
List b1=Arrays.asList("x1","x5");
List b2=Arrays.asList("x5","x2");
List b3=Arrays.asList("x2","x4");
List b4=Arrays.asList("x2","x2");
List b5=Arrays.asList("x2","x9");
List b6=Arrays.asList("x9","x9");
// queries.add(b1);
// queries.add(b2);
queries.add(b3);
// queries.add(b4);
// queries.add(b5);
// queries.add(b6);
Solution399Diy solution3991=new Solution399Diy();
double\[\] result=solution3991.calcEquation(equations,values,queries);
System.out.println("rersult");
System.out.println(result);
}
}
`