题目如下:

Equations are given in the format A / B = k, whereA 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<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries , where equations.size() == values.size(), and the values are positive. This represents the equations. Return vector<double>.

According to the example above:

  1. equations = [ ["a", "b"], ["b", "c"] ],
  2. values = [2.0, 3.0],
  3. 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.

解题思路:我的方法是先计算equations中所有的表达式,算出所有能间接得到的值存入字典中。最后判断queries中的值是否存在于字典中即可。例如['a','b']和['a','c']两个组合,两者相除即可得到['c','b']的组合。

代码如下:

  1. class Solution(object):
  2. def calcEquation(self, equations, values, queries):
  3. """
  4. :type equations: List[List[str]]
  5. :type values: List[float]
  6. :type queries: List[List[str]]
  7. :rtype: List[float]
  8. """
  9. dic_char = {}
  10. dic = {}
  11. queue = []
  12. for inx,(x,y) in enumerate(equations):
  13. queue.append((x,y))
  14. dic[(x,y)] = values[inx]
  15.  
  16. while len(queue) > 0:
  17. x,y = queue.pop(0)
  18. for inx, (m,n) in enumerate(equations):
  19. dic_char[m] = 1
  20. dic_char[n] = 1
  21. dic[(m, n)] = values[inx]
  22. if (x == m and y == n) or (x == n and y == m):
  23. continue
  24. elif x == m:
  25. if (y,n) not in dic and (n,y) not in dic:
  26. dic[(n,y)] = dic[(x,y)] / values[inx]
  27. queue.append((n,y))
  28. elif x == n:
  29. if (m,y) not in dic or (y,m) not in dic:
  30. dic[(m,y)] = dic[(x,y)] * values[inx]
  31. queue.append((m,y))
  32. elif y == m :
  33. if (x,n) not in dic and (n,x) not in dic:
  34. dic[(x,n)] = dic[(x,y)] * values[inx]
  35. queue.append((x,n))
  36. elif y == n:
  37. if (x,m) not in dic and (m,x) not in dic:
  38. dic[(x,m)] = dic[(x,y)] / values[inx]
  39. queue.append((x,m))
  40. #print dic
  41. #print dic_char
  42.  
  43. res = []
  44. for (x,y) in queries:
  45. if x not in dic_char or y not in dic_char:
  46. res.append(-1.0)
  47. elif x == y:
  48. res.append(1)
  49. elif (x,y) in dic:
  50. res.append(dic[(x,y)])
  51. elif (y,x) in dic:
  52. res.append(float(1.0)/float(dic[(y,x)]))
  53. else:
  54. res.append(-1.0)
  55. return res

【leetcode】399. Evaluate Division的更多相关文章

  1. 【LeetCode】399. Evaluate Division 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)

    [LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...

  3. 【LeetCode】553. Optimal Division 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. 【LeetCode】150. Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  5. 【leetcode】553. Optimal Division

    题目如下: 解题思路:这是数学上的一个定理.对于x1/x2/x3/..../xN的序列,加括号可以得到的最大值是x1/(x2/x3/..../xN). 代码如下: class Solution(obj ...

  6. LN : leetcode 399 Evaluate Division

    lc 399 Evaluate Division 399 Evaluate Division Equations are given in the format A / B = k, where A ...

  7. 【LeetCode】227. Basic Calculator II 解题报告(Python)

    [LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  8. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  9. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

随机推荐

  1. 微信小程序的事件

    事件,视图层到逻辑层的一种通讯方式,或者将用户的行为返回到逻辑层,当我们在组件绑定事件之后,当我们触发事件,就会执行逻辑层绑定的事件,处理回调函数,当页面的事件触发之后 页面上元素一些额外事件,通过事 ...

  2. vue2.0 之 douban (五)创建cell,media-cell组件

    1.组件cell 这里的cell分为三种样式,左侧带图标,不带图标,以及左侧带竖线的cell. 每一个组件都有一个底部边框: 这里我们采用了移动端1px像素问题的解决方法:父级元素设置相对定位,构建1 ...

  3. spring-cloud zuul网关

    API Gateway 是随着微服务(Microservice)这个概念一起兴起的一种架构模式,它用于解决微服务过于分散,没有一个统一的出入口进行流量管理的问题. 使用 Zuul 实现 API Gat ...

  4. ceph-性能调优

    Ceph 参数性能调优https://blog.csdn.net/changtao381/article/details/49907115这篇文章对我的环境有较大帮助 ceph优化记录 ceph.co ...

  5. jmeter之图片上传

    用jmeter来实现图片上传请求 目录 1.抓取参数 2.填写参数 1.抓取参数 第一步:先用fiddler抓取上传接口的参数 2.填写参数 第一步:在jmeter的参数列填写没有filename的这 ...

  6. msyql join语句执行原理

    首先,我建了一个表t2,里面有1000条数据,有id,a,b三个字段,a字段加了索引 然后我又建立一个t1表,里面有100条数据,和t2表的前一百条数据一致,也是只有id,a,b三个字段,a字段加了索 ...

  7. Postman初接触

    https://www.getpostman.com/docs/postman/launching_postman/installation_and_updates

  8. java_第一年_JavaWeb(8)

    前面说到,JSP在运行时会被编译成Servlet源代码,通过_jspServlet方法处理请求,此时该方法会传递和提供9个与web开发相关的对象进行使用,开发人员在JSP页面通过对这些变量即可引用这9 ...

  9. [暑假集训Day3T1]小木棍

    经典搜索题. 考虑以下9种优化 1)按木棍长度排序,使得较大长度的木棍被较早的选出. 2)只找能够整除的木棍长度,因为不能被sum整除一定不会出整数根,自然也就不是最优解. 3)枚举木棍长度时只需从最 ...

  10. [Python] 迭代器是什么?你每天在用的for循环都依赖它!

    从循环说起 顺序,分支,循环是编程语言的三大逻辑结构,在Python中都得到了支持,而Python更是为循环结构提供了非常便利的语法:for ... in ... 刚从C语言转入Python的同学可能 ...