题意理解:连乘的问题,链式元素,广度搜索
两个数字看做一条边,每条边有权重;给定两个节点,求出两个节点之间所有链路权重的乘积。
题目剖析:1.先将有向权重关系利用 Map<String,List<Node1,Node2>> 表示出来
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<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.
`
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<String, List<Edge>> 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<String> 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<mEdges.get(nodeStart).size();i++){
Edge edge=mEdges.get(nodeStart).get(i);
if(visited.contains(edge.to)){
continue;
}
visited.add(nodeStart);
double dfsValue=this.dfs(edge.to,nodeTo,visited);
if(dfsValue==-1.0) {
// return -1.0;
continue;
}else {
return edge.value\*dfsValue;
}
}
return \-1.0;
}
public double\[\] calcEquation(List<List<String>> equations, double\[\] values, List<List<String>> queries) {
for(int i=0;i<equations.size();i++){
Edge edge1=new Edge(equations.get(i).get(0),equations.get(i).get(1),values\[i\]);
if(!mEdges.containsKey(equations.get(i).get(0))){
List<Edge> 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<Edge> 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<String> visited=new ArrayList<>();
for(int i=0;i<queries.size();i++){
visited.clear();
double vals=this.dfs(queries.get(i).get(0),queries.get(i).get(1),visited);
mRes\[i\]=vals;
}
return mRes;
}
public static void main(String\[\] args){
Solution399Diy solution399=new Solution399Diy();
// List<List<String>> equations =new ArrayList<>({"a", "b"},{"b", "c"});
// List<List<String>> equations =\[\["a", "b"\],\["b", "c"\]\];
// List<String> equations =Arrays.asList("a", "b","b", "c");
// List<List<String>> equations =new ArrayList<>();
// List<String> a1= Arrays.asList("a","b");
// List<String> 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<List<String>> queries=new ArrayList<>();
// List<String> b1=Arrays.asList("a","c");
// List<String> b2=Arrays.asList("b","a");
// List<String> b3=Arrays.asList("a","e");
// List<String> b4=Arrays.asList("a","a");
// List<String> 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<List<String>> equations =new ArrayList<>();
List<String> a1= Arrays.asList("x1","x2");
List<String> a2=Arrays.asList("x2","x3");
List<String> a3=Arrays.asList("x3","x4");
List<String> 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<List<String>> queries=new ArrayList<>();
List<String> b1=Arrays.asList("x1","x5");
List<String> b2=Arrays.asList("x5","x2");
List<String> b3=Arrays.asList("x2","x4");
List<String> b4=Arrays.asList("x2","x2");
List<String> b5=Arrays.asList("x2","x9");
List<String> 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);
}
}
`
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。