LeetCode 399. Evaluate Division--Python-DFS解法
题目地址:Evaluate Division - LeetCode
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 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& values, vector<pair<string, string>> queries , where equations.size() == values.size(), and the values are positive. This represents the equations. Return vector.
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"] ].
这道题目很好理解,就是有向图的一个权值问题。
解法是从起点开始,使用DFS,或者BFS遍历图,思路跟这道题目很像:LeetCode 207. Course Schedule–有向图找环–面试算法题–DFS递归,拓扑排序迭代–Python
Python-DFS解法如下:
class Solution:def calcEquation(self, equations: List[List[str]], values: List[float], queries: List[List[str]]) -> List[float]:graph = {}for (x, y), v in zip(equations, values):if x in graph:graph[x][y] = velse:graph[x] = {y: v}if y in graph:graph[y][x] = 1/velse:graph[y] = {x: 1/v}def dfs(s, t) -> int:if s not in graph:return -1if t == s:return 1for node in graph[s].keys():if node == t:return graph[s][node]elif node not in visited:visited.add(node) # 添加到已访问避免重复遍历v = dfs(node, t)if v != -1:return graph[s][node]*vreturn -1res = []for qs, qt in queries:visited = set()res.append(dfs(qs, qt))return res
LeetCode 399. Evaluate Division--Python-DFS解法相关推荐
- leetcode 399. Evaluate Division | 399. 除法求值(图的邻接表,DFS)
题目 https://leetcode.com/problems/evaluate-division/ 题解 题目看着复杂,实际上是图的 DFS. 例如,已知 a/b, b/c, c/d,想求 a/d ...
- 利用哈希表和dfs解决LeetCode 399. Evaluate Division
问题简介 给定一些由变量组成的等式组,然后根据这些等式推算出所闻的等式的结果,如果无法推算,则返回-1.0. 比如: 给定等式组 a / b = 2.0, b / c = 3.0 求出 a / c = ...
- LeetCode 200. Number of Islands--c++ dfs解法
LeetCode 200. Number of Islands LeetCode题解专栏:LeetCode题解 LeetCode 所有题目总结:LeetCode 所有题目总结 大部分题目C++,Pyt ...
- LeetCode 85. Maximal Rectangle --python,java解法
题目地址: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1 ...
- leetcode 93.复原IP地址 dfs解法
给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式. 示例: 输入: "25525511135" 输出: ["255.255.11.135", ...
- LeetCode刷题之python解法(持续更新)
1. Two Sum 4行 class Solution:def twoSum(self, nums: List[int], target: int) -> List[int]:d = {}fo ...
- LeetCode 929 Unique Email Addresses--python一行解法,Java解法
题目地址:Unique Email Addresses - LeetCode Every email consists of a local name and a domain name, separ ...
- (最优解法)46行代码AC_HDU1242 Rescue(DFS解法+BFS解法)
励志用少的代码做高效表达 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. T ...
- LeetCode 其他部分 简单 Python实现
#LeetCode 其他部分 简单 Python实现 ''' 位1的个数 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 '1' 的个数(也被称为汉明重量).示例 : 输入: 11 ...
最新文章
- Windows系统内存分析工具的介绍
- 【 C 】经典抽象数据类型(ADT)之内存分配
- Javapinyin4J实现中文名转拼音
- JavaScript随机生成颜色以及十六进制颜色 与RGB颜色值的相互转换
- 理解 ActivityExecutionContextManager
- 《网络风险及网络安全》培训总结
- 2021年重庆市高考成绩查询时间复核,2021年重庆高考怎么查询是否被录取,具体录取时间安排...
- java+widthstep_关于IplImage中widthstep的大小与width,nchannels等的关系的问题
- 逍遥模拟器android4.0版本,逍遥安卓模拟器工作室版
- 如何查找期刊是否被SCI或者SSCI收录
- python3下载网页视频_三种下载网页视频的的方法(亲测!非常详细)
- 干货 | 闪电网络应用开发速成指南
- 【C++】1070:人口增长(信息学奥赛)
- 如何利用Spring Boot框架开发一个全球化的应用程序
- HTML图片映射矩形坐标,HTML图片热区map area的用法(转载)
- 浅谈前端的数据驱动和事件驱动
- 如何标题编号自动生成_标题编号的自动生成+自动修改的方法步骤
- 机器学习之变分推断(三)基于平均场假设变分推断与广义EM
- mysql连接查询、自查询
- 二进制表示负数的方法:“ 补数 ”
热门文章
- Python XML的解析与创建
- html5页面默认的字符集是什么,HTML 字符集
- 如何在golang中关闭bufio.reader_Golang 并发模型系列:1. 轻松入门流水线模型
- R 包 optparse 之命令行参数传递
- 无需写代码的高颜值富集分析神器
- ISME Commun: 华中农业大学在菌群收敛机制方面取得新进展
- 2019微生物组—宏基因组分析技术专题研讨会第四期
- R语言数据包自带数据集之survival包的colon数据集字段解释、数据导入实战
- R语言ggplot2可视化:水平半小提琴图(Horizontal Half Violin Plots)、去除水平半小提琴图中的填充色、ggridges包的绘制山脊图
- R语言gganimate包创建可视化gif动图、可视化动图:创建动态散点图动画基于transition_time函数、使用shadow_wake函数配置动画的渐变效果(gradual falloff)