【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 ...
随机推荐
- Nexus Repository OSS 3安装配置使用
Nexus Repository OSS 3是一个开源的仓库管理系统,提供了更加丰富的功能,而且安装.配置.使用起来也更加简单方便.OSS 3版本主要支持的仓库(Repository)包括如下: bo ...
- [CSP-S模拟测试]:题(DP)
题目描述 由于出题人赶时间所以没办法编故事来作为背景.一开始有$n$个苹果,$m$个人依次来吃苹果,第$i$个人会尝试吃$u_i$或$v_i$号苹果,具体来说分三种情况.$\bullet 1.$两个苹 ...
- IBatis.Net 下使用SqlBulkCopy 大批量导入数据 问题解决
SQLBulkCopy是继承SQLClient空间下的一个特殊类,它可以帮助我们以映射的方式把DataTable和DataReader数据大批量导入到数据库对应表中 public void Inert ...
- composer proc_open(): fork failed – Cannot allocate memory
一般小的VPS 才1G内存,如果使用composer会提示内存不足的现象 解决办法,可以使用交换内存 直接命令 /bin/dd if=/dev/zero of=/var/swap.1 bs=1M co ...
- vue2.0 之 douban (六)axios的简单使用
由于项目中用到了豆瓣api,涉及到跨域访问,就需要在config的index.js添加代理,例如 proxyTable: { // 设置代理,解决跨域问题 '/api': { target: 'htt ...
- Java多线程,实现卖电影票的业务
本篇重点:多线程共享资源时发生的互斥问题 一般的我们售卖电影票或者火车票时会有多个窗口同时买票, 我们来看测试代码:主方法new一个Ticket(一个堆),之后三个线程来启动(三个窗口买票) clas ...
- Log4j log for java(java的日志) 的使用
log4j的使用,Log4j log for java(java的日志) 是java主流的日志框架,提供各种类型,各种存储,各种格式,多样化的日志服务. 可以再Apache官网下载得到. 我们下载lo ...
- 测开之路七十一:监控平台之js
监控平台的js //datetimepicker的初始化函数(主要是对选择时间的下拉框)function init_datetimepicker() { //初始化格式和规则 $('#start'). ...
- 关于Python的10大实用编程技巧
Python 是一种通用的脚本开发语言,比其他编程语言更加简单.易学,其面向对象特性甚至比Java.C#..NET更加彻底,因此非常适合快速开发. Python 已经成为最受欢迎的程序设计语言之一 ...
- CentOS安装ruby, Haskall,io语言
安装ruby yum install ruby irb rdoc 安装Haskall yum install ghc 安装io语言 安装io语言,需要先安装cmake不过不要使用yum来进行安装,yu ...