399. Evaluate Division
Posted tobeabetterpig
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了399. Evaluate Division相关的知识,希望对你有一定的参考价值。
399. Evaluate Division https://www.youtube.com/watch?v=UwpvInpgFmo // huahua’s code class Solution { Map<String, HashMap<String, Double>> g = new HashMap<>(); public double[] calcEquation(String[][] equations, double[] values, String[][] queries) { for (int i = 0; i < equations.length; ++i) { String x = equations[i][0]; String y = equations[i][1]; double k = values[i]; g.computeIfAbsent(x, l -> new HashMap<String, Double>()).put(y, k); g.computeIfAbsent(y, l -> new HashMap<String, Double>()).put(x, 1.0 / k); } double[] ans = new double[queries.length]; for (int i = 0; i < queries.length; ++i) { String x = queries[i][0]; String y = queries[i][1]; if (!g.containsKey(x) || !g.containsKey(y)) { ans[i] = -1.0; } else { ans[i] = divide(x, y, new HashSet<String>()); } } return ans; } private double divide(String x, String y, Set<String> visited) { if (x.equals(y)) return 1.0; visited.add(x); // if (!g.containsKey(x)) return -1.0; for (String n : g.get(x).keySet()) { if (visited.contains(n)) continue; visited.add(n); double d = divide(n, y, visited); if (d > 0) return d * g.get(x).get(n); } return -1.0; } } // not passed class Solution { // class Pair{ // String var; // double distance; // public Pair(String nei, double distance){ // this.nei = var; // this.distance = distance; // } // } public double[] calcEquation(String[][] equations, double[] values, String[][] queries) { double[] res = new int[queries.length]; // build the graph using hashtable with information from eq and values HashMap<String, HashMap<String, Double> map = new HashMap<>(); // equations = [ ["a", "b"], ["b", "c"] ], // values = [2.0, 3.0], for(int i = 0; i < values; i++){ String[] eq = equations[i]; double value = values[i]; String from = eq[0]; String to = eq[1]; // fill in the map if(!map.containsKey(from)){ map.put(from, new ArrayList<>()); } map.get(from).add()); if(!map.containsKey(to)){ map.put(to, new ArrayList<>()); } map.get(to).add(new Pair(from, 1 / value)); } for(int i = 0; i < queries.length; i++){ String from = queries[i][0]; String to = queries[i][1]; // early pruning check if(!map.containsKey(from) || !map.containsKey(to)){ res[i] = -1.0; }else if(from.equals(to)){ res[i] = 1.0; }else{ // regular dfs double product = 1.0; HashSet<String> visited = new HashSet<>(); visited.add(from); res[i] = dfs(from, to, map, product, visited); } } return res; } private double dfs(String from, String to, HashMap<Pair<String, double>> map, double product, HashSet<String> visited){ // regular dfs for(Pair<> pair : map.get(from)){ String nei = pair.nei; double distance = pair.distance; // if we dound the destination already if(nei.equals(to)){ product = product * distance; return product; } // else we keep going down this path until we found the destination // based on the assumption that this is one connected component // ? how to handle the case when the nei is visited already // i know we do nothing , but on the implementation level // do we need to write anything ? if(visited.contains(nei)) { continue; }else{ visited.add(nei); product = product * distance; dfs(nei, to, map, product, visited); } } // else we could not find the return -1; } } You can make it like : List<Object> sections = new ArrayList <Object>(); (Recommended) Another possible solution would be to make a custom model class with two parameters one Integer and other String. Then using an ArrayList of that object.
以上是关于399. Evaluate Division的主要内容,如果未能解决你的问题,请参考以下文章