Evaluate Division——LeetCode进阶路
原题链接https://leetcode.com/problems/evaluate-division/
题目描述
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”] ].
The input is always valid. You may assume that evaluating the queries will result in no division by zero and there is no contradiction.
思路分析
题目大意:给出一些变量的比值,求给出的变量比值,若给出变量比值中有未出现过的变量,返回-1.0
分析:用HashMap存储图的链接表,用Set创建图节点的访问标记,搜索是否有满足关系的路径(笔者使用dfs)。当然使用Floyd算法(就是插点法啦)也OK的,就是他的O(n^3)d的时间复杂度……
- 其中需要注意以下几种特殊情况:
- a \ a =1自身相除为1
- 不连通的情况,返回-1
AC解
class Solution {
Map<String,HashMap<String,Double>> map = new HashMap<>();
public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
for(int i=0; i<equations.length;i ++)
{
String s1 = equations[i][0];
String s2 = equations[i][1];
double d = values[i];
map.computeIfAbsent(s1, l -> new HashMap<String, Double>()).put(s2, d);
map.computeIfAbsent(s2, l -> new HashMap<String, Double>()).put(s1, 1.0/d);
}
double[] result = new double[queries.length];
for(int i=0; i<queries.length;i ++)
{
String s1 = queries[i][0];
String s2 = queries[i][1];
if(!map.containsKey(s1) || !map.containsKey(s2) )
{
result[i] = -1.0;
}
else
{
result[i] = f(s1,s2,new HashSet<String>());
}
}
return result;
}
public double f(String s1,String s2,Set<String> set)
{
if(s1.equals(s2))
{
return 1.0;
}
set.add(s1);
if(!map.containsKey(s1))
{
return -1.0;
}
for(String t:map.get(s1).keySet())
{
if(set.contains(t))
{
continue;
}
set.add(t);
double d = f(t,s2,set);
if(d>0)
{
return d*map.get(s1).get(t);
}
}
return -1.0;
}
}
Evaluate Division——LeetCode进阶路的更多相关文章
- [Leetcode Week3]Evaluate Division
Evaluate Division题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/evaluate-division/description/ Desc ...
- LN : leetcode 399 Evaluate Division
lc 399 Evaluate Division 399 Evaluate Division Equations are given in the format A / B = k, where A ...
- 【LeetCode】399. Evaluate Division 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode] Evaluate Division 求除法表达式的值
Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...
- [LeetCode] 399. Evaluate Division 求除法表达式的值
Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...
- Leetcode: Evaluate Division
Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...
- [leetcode] 399. Evaluate Division
我是链接 看到这道题,2个点和一个权值,然后想到图,但是leetcode就是这样,没给数据范围,感觉写起来很费劲,然后就开始用图来做,添加边的时候,注意正向边和反向变,然后查询的时候,先判断2个点是否 ...
- 【leetcode】399. Evaluate Division
题目如下: Equations are given in the format A / B = k, whereA and B are variables represented as strings ...
- 阿里巴巴 web前端性能优化进阶路
Web前端性能优化WPO,相信大多数前端同学都不会陌生,在各自所负责的站点页面中,也都会或多或少的有过一定的技术实践.可以说,这个领域并不缺乏成熟技术理论和技术牛人:例如Yahoo的web站点性能优化 ...
- [Swift]LeetCode399. 除法求值 | Evaluate Division
Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...
随机推荐
- Typecho Mirages 主题自定义公告样式
使用步骤 将以下代码加入到 <head> 标签中.对于本主题,依次进入 控制台 - 外观 - 设置外观 - 主题自定义扩展,将代码加入到 自定义 HTML 元素拓展 - 标签: head ...
- mysql 获取数据库名、表名、字段名、根据表结构创建新表
1.查询当前使用的数据库 select database(): 2.获取当前数据库表 select * from information_schema.TABLES where TABLE_SCHEM ...
- day2-变量与数据类型
变量 概念:程序的基本组成单位 定义: 指定变量类型 根据值自行判断变量类型(类型推导) 省略var,定义赋值 var i int var i = 10 i, j := 20, 10 数据基本类型 基 ...
- manim边学边做--向量相关的场景类
VectorScene是Manim动画库中专门用于向量空间可视化的场景类,继承自基础 Scene 类. 它通过封装一系列向量操作方法,使数学教育.物理模拟等领域的动画制作更加高效. 本文主要介绍Vec ...
- go mod 安装使用 beego
go module基本使用 // 创建目录,初始化新项目 mkdir beemod cd beemod go mod init beemod 创建 server.go 文件 package main ...
- OSPF各类LSA
一.域内路由 路由器将接口宣告进OSPF进程后,形成的链路状态放入1类LSA中,用于描述路由器自身的直连状态. 1. 区域0为骨干区域,非0为非骨干区域. 2. 骨干区域有且只能存在一个. 3. 非骨 ...
- oracle的各版本的名称
我最早接触的是oracle的版本8那个时候是8i i是internet后来是9i然后到10,就是版本10g g是grid的意思然后是11g然后12就变成了C,就是12c c是cloud的意思然后后面的 ...
- Linux reboot全过程
一.版本说明嵌入式Linux 下面的reboot命令看似简单,但出问题时定位起来发现别有洞天.下面就按在shell下执行reboot命令之后程序的执行过程进行解析.Busybox:1.23.2 ...
- VS Code Runner 插件配置
VS Code Runner 插件配置 Code Runner插件是一个小而美的插件,可以很方便的运行一些简单的代码文件. 本篇博文记录一些相关的环境配置. 设置C++编译标准 这里可以设置默认的C+ ...
- nacos(九):sentinel——规则持久化
接上回,sentinel基本使用我们已经掌握.但是在设置限流规则时,会发现规则都是临时的,一段时间没访问资源或者重启sentinel,规则就会消失.所以,我们需要有一个将规则持久化保存的地方,让规则一 ...