Evaluate Division | codesolutiony
Evaluate Division 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. public class Solution { public double[] calcEquation(String[][] equations, double[] values, String[][] queries) { Map
> map = new HashMap>(); for (int i = 0; i < equations.length; i++) { String[] equation = equations[i]; String a = equation[0]; String b = equation[1]; if (!map.containsKey(a)) { Map innerMap = new HashMap(); map.put(a, innerMap); } map.get(a).put(b, values[i]); if (!map.containsKey(b)) { Map innerMap = new HashMap(); map.put(b, innerMap); } map.get(b).put(a, 1/values[i]); } double[] results = new double[queries.length]; for (int i = 0; i < queries.length; i++) { String[] query = queries[i];
Read full article from Evaluate Division | codesolutiony
No comments:
Post a Comment