Problem
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
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.
Solution - Updated Union Findclass Solution { public double[] calcEquation(String[][] equations, double[] values, String[][] queries) { MapSolution - DFSgraph = new HashMap<>(); Map ratio = new HashMap<>(); double[] res = new double[queries.length]; for (int i = 0; i < equations.length; i++) { String p0 = find(equations[i][0], graph, ratio); String p1 = find(equations[i][1], graph, ratio); graph.put(p0, p1); ratio.put(p0, values[i] * ratio.get(equations[i][1]) / ratio.get(equations[i][0])); } for (int i = 0; i < queries.length; i++) { if (!graph.containsKey(queries[i][0]) || !graph.containsKey(queries[i][1])) { res[i] = -1.0; continue; } String p0 = find(queries[i][0], graph, ratio); String p1 = find(queries[i][1], graph, ratio); if (!p0.equals(p1)) { res[i] = -1.0; continue; } res[i] = ratio.get(queries[i][0]) / ratio.get(queries[i][1]); } return res; } private String find(String str, Map graph, Map ratio) { if (!graph.containsKey(str)) { graph.put(str, str); ratio.put(str, 1.0); return str; } if (graph.get(str).equals(str)) return str; String parent = graph.get(str); String ancestor = find(parent, graph, ratio); graph.put(str, ancestor); ratio.put(str, ratio.get(str)*ratio.get(parent)); return ancestor; } }
class Solution { public double[] calcEquation(String[][] equations, double[] values, String[][] queries) { double[] res = new double[queries.length]; SetSolution - Union Finddict = new HashSet<>(); for (String[] pair: equations) { dict.add(pair[0]); dict.add(pair[1]); } for (int i = 0; i < queries.length; i++) { String[] pair = queries[i]; if (!dict.contains(pair[0]) || !dict.contains(pair[1])) { res[i] = -1.0d; } else { res[i] = dfs(equations, values, pair, new HashSet ()); } } return res; } private double dfs(String[][] equations, double[] values, String[] pair, Set set) { for (int i = 0; i < equations.length; i++) { if (pair[0].equals(equations[i][0]) && pair[1].equals(equations[i][1])) return values[i]; if (pair[0].equals(equations[i][1]) && pair[1].equals(equations[i][0])) return 1.0d/values[i]; } for (int i = 0; i < equations.length; i++) { if (!set.contains(i) && pair[0].equals(equations[i][0])) { set.add(i); double temp = dfs(equations, values, new String[]{equations[i][1], pair[1]}, set)*values[i]; if (temp > 0) return temp; else set.remove(i); } if (!set.contains(i) && pair[0].equals(equations[i][1])) { set.add(i); double temp = dfs(equations, values, new String[]{equations[i][0], pair[1]}, set)/values[i]; if (temp > 0) return temp; else set.remove(i); } } return -1.0d; } }
class Solution { public double[] calcEquation(String[][] equations, double[] values, String[][] queries) { Mapgraph = new HashMap<>(); Map ratio = new HashMap<>(); for (int i = 0; i < equations.length; i++) { union(graph, ratio, equations[i][0], equations[i][1], values[i]); } double[] res = new double[queries.length]; for (int i = 0; i < queries.length; i++) { String s1 = queries[i][0], s2 = queries[i][1]; if (!graph.containsKey(s1) || !graph.containsKey(s2) || !find(graph, ratio, s1).equals(find(graph, ratio, s2))) { res[i] = -1.0d; } else { res[i] = ratio.get(s1)/ratio.get(s2); } } return res; } private void union(Map graph, Map ratio, String s1, String s2, double value) { if (!graph.containsKey(s1)) { graph.put(s1, s1); ratio.put(s1, 1.0d); } if (!graph.containsKey(s2)) { graph.put(s2, s2); ratio.put(s2, 1.0d); } String p1 = find(graph, ratio, s1); String p2 = find(graph, ratio, s2); graph.put(p1, p2); ratio.put(p1, value*ratio.get(s2)/ratio.get(s1)); } private String find(Map graph, Map ratio, String str) { if (str.equals(graph.get(str))) return str; String parent = graph.get(str); String ancestor = find(graph, ratio, parent); graph.put(str, ancestor); ratio.put(str, ratio.get(str)*ratio.get(parent)); return ancestor; } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/72140.html
摘要:題目要求已知一些字母之間的關系式,問是否能夠計算出其它字母之間的倍數關系如已知問是否能夠計算出的值。這里的值因為在條件中無法獲知是否等于零,因此也無法計算其真實結果,也需要返回。即代表點指向點的邊的權重為,而點指向點的邊的全中為。 題目要求 Equations are given in the format A / B = k, where A and B are variables ...
摘要:建好圖之后就是查找了,圖里面查找用或者都可以,寫起來簡單點。復雜度沒什么差別都是,這道題里面最多是,所以每次查找的復雜度是,有次查找。注意防止重復路徑,要用。 399. Evaluate Division 題目鏈接:https://leetcode.com/problems... 無向圖里找路徑的問題,用鄰接鏈或者鄰接矩陣來建圖,用鄰接鏈的話注意兩個方向,a/b的時候,既要把b加到a的...
Problem Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Note: Division between two inte...
摘要:題目根據逆波蘭表示法,求表達式的值。給定逆波蘭表達式總是有效的。逆波蘭表達式又叫做后綴表達式。解題思路可以看出逆波蘭表達式中的每一個運算符屬于該運算符前的兩個數字間的運算。如如波蘭表達式則加號前兩個數字為。 題目: 根據逆波蘭表示法,求表達式的值。 有效的運算符包括 +, -, *, / 。每個運算對象可以是整數,也可以是另一個逆波蘭表達式。 Evaluate the value of...
摘要:小鹿題目根據逆波蘭表示法,求表達式的值。給定逆波蘭表達式總是有效的。算法思路仔細觀察上述的逆波蘭表達式,可以發現一個規律就是每遇到一個操作符,就將操作符前的兩個操作數進行運算,將結果保存到原位置。 Time:2019/4/14Title: Evaluate Reverse Polish NotationDifficulty: MediumAuthor:小鹿 題目:Evaluate ...
閱讀 3518·2023-04-25 17:35
閱讀 2594·2021-11-24 09:39
閱讀 2530·2021-10-18 13:32
閱讀 3416·2021-10-11 10:58
閱讀 1637·2021-09-26 09:55
閱讀 6152·2021-09-22 15:47
閱讀 967·2021-08-26 14:15
閱讀 3472·2019-08-30 15:55