题目如下:

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:

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的更多相关文章

  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. python之面向过程,函数式编程,面向对象浅析

    python编程有面向过程.面向函数.面向对象三种,那么他们区别在哪呢?这个问题,让我想起我在学习编程的时候,我的老师给我举的例子.分享给大家. 面向过程就是将编程当成是做一件事,要按步骤完成! 比如 ...

  2. (转)原理到实现 | K8S 存储之 NFS

    转:https://mp.weixin.qq.com/s/Mrr1Rnl_594Gyyn9fHekjw 1NFS介绍 NFS是Network File System的简写,即网络文件系统,NFS是Fr ...

  3. iOS逆向一个APP指令集

    一.脱壳获取.app文件 1.查询壳有没加密 otool -l  mac-o文件 | grep crypt 2.Clutch -i Clutch -d  num 3.脱壳后的位置 /private/v ...

  4. (\w+)\s*, \s*(\w+)

    \s表示空格 \w表示任何字符,字母数字下划线 _就表示下划线

  5. php面向对象三大特性

    1.封装: 目的:使类更加安全 步骤:1.成员变量变成private(私有的)2.设置方法/调用方法3.在方法中增加限制 <?php class shao { private $aa;//必须是 ...

  6. Nginx 实现全站 HTTPS(基于 Let's Encrypt 的免费通配符证书)

    单域名证书的生成可以 参考这里. acme.sh 项目中文文档 Let's Encrypt 在 18 年 1 月份推出了 ACME v2,支持通配符域名证书,对小网站.个人站长的友好度进一步增加. 常 ...

  7. Vagrant 手册之 Provisioning - file 配置程序

    原文地址 Provisioner 命令:"file" 通过 file 配置程序可以上传宿主机的文件或目录到虚拟机中. 使用场景:将宿主机的 ~/.gitconfig 复制到虚拟机中 ...

  8. 16/7/14-MySQL-修改mysql5.6以上版本root密码

    版本更新,原来user里的password字段已经变更为authentication_string 版本更新 缘故,好多网上的教程都不适用了,甚至连官网的文档也不是能够顺利操作的. 如果 MySQL ...

  9. Oracle11g安装步骤

    plsql安装等:https://blog.csdn.net/li66934791/article/details/83856225      https://www.cnblogs.com/gaoz ...

  10. Support Vector Machine(1):线性可分集的决策边界

    与Logistuc Regression相比,SVM是一种优化的分类算法,其动机是寻找一个最佳的决策边界,使得从决策边界与各组数据之间存在margin,并且需要使各侧的margin最大化.比较容易理解 ...