资讯专栏INFORMATION COLUMN

399. Evaluate Division

yanest / 2560人阅读

摘要:建好图之后就是查找了,图里面查找用或者都可以,写起来简单点。复杂度没什么差别都是,这道题里面最多是,所以每次查找的复杂度是,有次查找。注意防止重复路径,要用。

399. Evaluate Division

题目链接:https://leetcode.com/problems...

无向图里找路径的问题,用邻接链或者邻接矩阵来建图,用邻接链的话注意两个方向,a/b的时候,既要把b加到a的邻接list里,也要把a加到b的邻接list里面。建好图之后就是查找了,图里面查找用bfs或者dfs都可以,dfs写起来简单点。复杂度没什么差别都是O(V+E),这道题里面E = equations.length, V最多是2E,所以每次查找的复杂度是O(equations.length),有queries.length次查找。注意防止重复路径,要用visited。

</>复制代码

  1. public class Solution {
  2. public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
  3. // build graph, use adjacent list
  4. map = new HashMap();
  5. for(int i = 0; i < equations.length; i++) {
  6. String[] equation = equations[i];
  7. if(!map.containsKey(equation[0])) map.put(equation[0], new ArrayList());
  8. map.get(equation[0]).add(new Info(equation[1], values[i]));
  9. if(!map.containsKey(equation[1])) map.put(equation[1], new ArrayList());
  10. map.get(equation[1]).add(new Info(equation[0], 1 / values[i]));
  11. }
  12. double[] result = new double[queries.length];
  13. for(int i = 0; i < result.length; i++) {
  14. result[i] = find(queries[i][0], queries[i][1], 1, new HashSet());
  15. }
  16. return result;
  17. }
  18. HashMap> map;
  19. private double find(String start, String end, double value, Set visited) {
  20. if(visited.contains(start)) return -1;
  21. if(!map.containsKey(start)) return -1;
  22. if(start.equals(end)) return value;
  23. visited.add(start);
  24. for(Info next : map.get(start)) {
  25. double sub = find(next.den, end, value * next.val, visited);
  26. if(sub != -1.0) return sub;
  27. }
  28. visited.remove(start);
  29. return -1;
  30. }
  31. class Info {
  32. String den;
  33. double val;
  34. Info(String den, double val) { this.den = den; this.val = val; }
  35. }
  36. }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66640.html

相关文章

  • leetcode399. Evaluate Division

    摘要:题目要求已知一些字母之间的关系式,问是否能够计算出其它字母之间的倍数关系如已知问是否能够计算出的值。这里的值因为在条件中无法获知是否等于零,因此也无法计算其真实结果,也需要返回。即代表点指向点的边的权重为,而点指向点的边的全中为。 题目要求 Equations are given in the format A / B = k, where A and B are variables ...

    Jensen 评论0 收藏0
  • [LeetCode] 399. Evaluate Division

    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 ...

    BlackMass 评论0 收藏0
  • [LeetCode] 150. Evaluate Reverse Polish Notation

    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...

    KoreyLee 评论0 收藏0
  • LeetCode 150:逆波兰表达式求值 Evaluate Reverse Polish Nota

    摘要:题目根据逆波兰表示法,求表达式的值。给定逆波兰表达式总是有效的。逆波兰表达式又叫做后缀表达式。解题思路可以看出逆波兰表达式中的每一个运算符属于该运算符前的两个数字间的运算。如如波兰表达式则加号前两个数字为。 题目: 根据逆波兰表示法,求表达式的值。 有效的运算符包括 +, -, *, / 。每个运算对象可以是整数,也可以是另一个逆波兰表达式。 Evaluate the value of...

    Turbo 评论0 收藏0
  • LeetCode 之 JavaScript 解答第150题 —— 逆波兰表达式求值

    摘要:小鹿题目根据逆波兰表示法,求表达式的值。给定逆波兰表达式总是有效的。算法思路仔细观察上述的逆波兰表达式,可以发现一个规律就是每遇到一个操作符,就将操作符前的两个操作数进行运算,将结果保存到原位置。 Time:2019/4/14Title: Evaluate Reverse Polish NotationDifficulty: MediumAuthor:小鹿 题目:Evaluate ...

    104828720 评论0 收藏0

发表评论

0条评论

yanest

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<