【leetcode】399. Evaluate Division
题目如下:
Equations are given in the format
A / B = k
, whereA
andB
are variables represented as strings, andk
is a real number (floating point number). Given some queries, return the answers. If the answer does not exist, return-1.0
.Example:
Givena / 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
, whereequations.size() == values.size()
, and the values are positive. This represents the equations. Returnvector<double>
.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.
解题思路:我的方法是先计算equations中所有的表达式,算出所有能间接得到的值存入字典中。最后判断queries中的值是否存在于字典中即可。例如['a','b']和['a','c']两个组合,两者相除即可得到['c','b']的组合。
代码如下:
- class Solution(object):
- def calcEquation(self, equations, values, queries):
- """
- :type equations: List[List[str]]
- :type values: List[float]
- :type queries: List[List[str]]
- :rtype: List[float]
- """
- dic_char = {}
- dic = {}
- queue = []
- for inx,(x,y) in enumerate(equations):
- queue.append((x,y))
- dic[(x,y)] = values[inx]
- while len(queue) > 0:
- x,y = queue.pop(0)
- for inx, (m,n) in enumerate(equations):
- dic_char[m] = 1
- dic_char[n] = 1
- dic[(m, n)] = values[inx]
- if (x == m and y == n) or (x == n and y == m):
- continue
- elif x == m:
- if (y,n) not in dic and (n,y) not in dic:
- dic[(n,y)] = dic[(x,y)] / values[inx]
- queue.append((n,y))
- elif x == n:
- if (m,y) not in dic or (y,m) not in dic:
- dic[(m,y)] = dic[(x,y)] * values[inx]
- queue.append((m,y))
- elif y == m :
- if (x,n) not in dic and (n,x) not in dic:
- dic[(x,n)] = dic[(x,y)] * values[inx]
- queue.append((x,n))
- elif y == n:
- if (x,m) not in dic and (m,x) not in dic:
- dic[(x,m)] = dic[(x,y)] / values[inx]
- queue.append((x,m))
- #print dic
- #print dic_char
- res = []
- for (x,y) in queries:
- if x not in dic_char or y not in dic_char:
- res.append(-1.0)
- elif x == y:
- res.append(1)
- elif (x,y) in dic:
- res.append(dic[(x,y)])
- elif (y,x) in dic:
- res.append(float(1.0)/float(dic[(y,x)]))
- else:
- res.append(-1.0)
- return res
【leetcode】399. Evaluate Division的更多相关文章
- 【LeetCode】399. Evaluate Division 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)
[LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...
- 【LeetCode】553. Optimal Division 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】150. Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- 【leetcode】553. Optimal Division
题目如下: 解题思路:这是数学上的一个定理.对于x1/x2/x3/..../xN的序列,加括号可以得到的最大值是x1/(x2/x3/..../xN). 代码如下: class Solution(obj ...
- LN : leetcode 399 Evaluate Division
lc 399 Evaluate Division 399 Evaluate Division Equations are given in the format A / B = k, where A ...
- 【LeetCode】227. Basic Calculator II 解题报告(Python)
[LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
随机推荐
- 微信小程序的事件
事件,视图层到逻辑层的一种通讯方式,或者将用户的行为返回到逻辑层,当我们在组件绑定事件之后,当我们触发事件,就会执行逻辑层绑定的事件,处理回调函数,当页面的事件触发之后 页面上元素一些额外事件,通过事 ...
- vue2.0 之 douban (五)创建cell,media-cell组件
1.组件cell 这里的cell分为三种样式,左侧带图标,不带图标,以及左侧带竖线的cell. 每一个组件都有一个底部边框: 这里我们采用了移动端1px像素问题的解决方法:父级元素设置相对定位,构建1 ...
- spring-cloud zuul网关
API Gateway 是随着微服务(Microservice)这个概念一起兴起的一种架构模式,它用于解决微服务过于分散,没有一个统一的出入口进行流量管理的问题. 使用 Zuul 实现 API Gat ...
- ceph-性能调优
Ceph 参数性能调优https://blog.csdn.net/changtao381/article/details/49907115这篇文章对我的环境有较大帮助 ceph优化记录 ceph.co ...
- jmeter之图片上传
用jmeter来实现图片上传请求 目录 1.抓取参数 2.填写参数 1.抓取参数 第一步:先用fiddler抓取上传接口的参数 2.填写参数 第一步:在jmeter的参数列填写没有filename的这 ...
- msyql join语句执行原理
首先,我建了一个表t2,里面有1000条数据,有id,a,b三个字段,a字段加了索引 然后我又建立一个t1表,里面有100条数据,和t2表的前一百条数据一致,也是只有id,a,b三个字段,a字段加了索 ...
- Postman初接触
https://www.getpostman.com/docs/postman/launching_postman/installation_and_updates
- java_第一年_JavaWeb(8)
前面说到,JSP在运行时会被编译成Servlet源代码,通过_jspServlet方法处理请求,此时该方法会传递和提供9个与web开发相关的对象进行使用,开发人员在JSP页面通过对这些变量即可引用这9 ...
- [暑假集训Day3T1]小木棍
经典搜索题. 考虑以下9种优化 1)按木棍长度排序,使得较大长度的木棍被较早的选出. 2)只找能够整除的木棍长度,因为不能被sum整除一定不会出整数根,自然也就不是最优解. 3)枚举木棍长度时只需从最 ...
- [Python] 迭代器是什么?你每天在用的for循环都依赖它!
从循环说起 顺序,分支,循环是编程语言的三大逻辑结构,在Python中都得到了支持,而Python更是为循环结构提供了非常便利的语法:for ... in ... 刚从C语言转入Python的同学可能 ...