@@ -0,0 +1,1741 @@
+// Another solution is actually creating Graph class to store vertices and edges and do DFS%0Apublic class Solution %7B%0A  public static class Edge %7B%0A    String to;%0A    double weight;%0A    Edge(String to, double weight) %7B%0A      this.to = to;%0A      this.weight = weight;%0A    %7D%0A  %7D%0A%0A  public double dfs(HashMap%3CString, List%3CEdge%3E%3E graph, String from, String to) %7B%0A    if (from == null %7C%7C to == null) return -1.0;%0A    if (!graph.containsKey(from) %7C%7C !graph.containsKey(to)) return -1.0;%0A    return dfs(graph, from, to, new HashSet%3CString%3E());%0A  %7D%0A%0A  private double dfs(HashMap%3CString, List%3CEdge%3E%3E graph, String from, String to, HashSet%3CString%3E visited) %7B%0A    if (from.equals(to)) return 1.0;%0A    if (visited.contains(from)) return -1.0;%0A    visited.add(from);%0A    for (Edge edge : graph.get(from)) %7B%0A      double res = dfs(graph, edge.to, to, visited);%0A      if (res != -1.0) return res * edge.weight;%0A    %7D%0A    return -1.0;%0A  %7D%0A%0A  public double%5B%5D calcEquation(String%5B%5D%5B%5D equations, double%5B%5D values, String%5B%5D%5B%5D queries) %7B%0A    // build graph%0A    HashMap%3CString, List%3CEdge%3E%3E graph = new HashMap%3CString, List%3CEdge%3E%3E();%0A    for (int i = 0; i %3C equations.length; i++) %7B%0A      String%5B%5D points = equations%5Bi%5D;%0A      double weight = values%5Bi%5D;%0A      if (!graph.containsKey(points%5B0%5D)) graph.put(points%5B0%5D, new ArrayList%3CEdge%3E());%0A      if (!graph.containsKey(points%5B1%5D)) graph.put(points%5B1%5D, new ArrayList%3CEdge%3E());%0A      graph.get(points%5B0%5D).add(new Edge(points%5B1%5D, weight));%0A      graph.get(points%5B1%5D).add(new Edge(points%5B0%5D, 1.0 / weight));%0A    %7D%0A%0A    double%5B%5D res = new double%5Bqueries.length%5D;%0A    for (int i = 0; i %3C queries.length; i++) %7B%0A      String%5B%5D points = queries%5Bi%5D;%0A      res%5Bi%5D = dfs(graph, points%5B0%5D, points%5B1%5D);%0A    %7D%0A    return res;%0A  %7D%0A%7D%0A

add evaluate-division solution