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 ...
随机推荐
- MySQL - [07] 查看库表数据所使用的空间大小
1.切换数据库:use information_schema; 2.查看数据库使用大小 SELECT concat(round(sum(data_length/1024/1024),2),'MB') ...
- mysql - 存储过程及分支语句
存储过程是预编译好的sql语言的集合 减少编译次数,提高sql语句的重用性.但是在现阶段因为其维护困难及其他一系列的原因,有些时候并不推荐使用 创建 create procedure 存储过程的名字 ...
- VRRP+BFD实验
VRRP(Virtual Router Redundancy Protocol,虚拟路由器冗余协议)的工作原理主要涉及多个路由器(或具备路由功能的设备)协同工作,通过VRRP报文和优先级机制来选举出一 ...
- 还原大师-遍历残缺字符串匹配md5杂凑值
题目: 我们得到了一串神秘字符串:TASC?O3RJMV?WDJKX?ZM,问号部分是未知大写字母, 为了确定这个神秘字符串,我们通过了其他途径获得了这个字串的32位MD5码. 但是我们获得它的32位 ...
- AccessibilityService-weditor获取节点元素信息&Assists实现自动化
1. weditor 简介 在使用AccessibilityService开发自动化工具或自动化测试脚本时需要知道控件属性才能自动化逻辑.weditor 是一个用于 Android UI 自动化测试的 ...
- 深入浅出CPU眼中的函数调用&栈溢出攻击
深入浅出CPU眼中的函数调用--栈溢出攻击 原理解读 函数调用,大家再耳熟能详了,我们先看一个最简单的函数: #include <stdio.h> #include <stdlib. ...
- etcd和Zookeeper孰优孰劣对比
背景 最近在看到Pachyderm的介绍时,看到作者拿YARN和Kubernetes做类比,拿Zookeeper和etcd做对比.YARN和Kubernetes的类比还相对比较好理解,毕竟他们都有资源 ...
- some notes
.displaynone { display: none } https://voce.chat/zh-CN 一个开源的迷你的国产开源聊天软件,服务端非常小,只有 15MB. 4 分钟前 虽然没有办法 ...
- WinDebug查看C#程序运行内存中的数据库连接字符串
真巧,昨天刷到了大佬"一线码农"的视频,大概就是讲的有人找他破解一个混淆加密的数据库连接字符串,然后大佬也提供了方案就是用WinDebug查看内存中的数据.这其实本质上就是一个用W ...
- pytorch 实战教程之 SPP(SPPNet---Spatial Pyramid Pooling)空间金字塔池化网络代码实现 和 SPPF (Spatial Pyramid Pooling Fast)详解
原文作者:aircraft 原文链接:pytorch 实战教程之 SPP(SPPNet---Spatial Pyramid Pooling)空间金字塔池化网络代码实现 和 SPPF (Spatial ...