LeetCode:197.上升的温度
题目
给定一个 Weather 表,编写一个 SQL 查询,来查找与之前(昨天的)日期相比温度更高的所有日期的 Id。
+---------+------------------+------------------+
| Id(INT) | RecordDate(DATE) | Temperature(INT) |
+---------+------------------+------------------+
| 1 | 2015-01-01 | 10 |
| 2 | 2015-01-02 | 25 |
| 3 | 2015-01-03 | 20 |
| 4 | 2015-01-04 | 30 |
+---------+------------------+------------------+
例如,根据上述给定的 Weather 表格,返回如下 Id:
+----+
| Id |
+----+
| 2 |
| 4 |
+----+
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/rising-temperature
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解答
第一感觉,用 oracle 中的分析函数,偏移函数 lag 、lead,不过不知道这里支不支持窗口函数,测试一番再说。
---- oracle ----
/* Write your PL/SQL query statement below */
select Id
from
(
select Id,
RecordDate,
Temperature,
lag(Temperature,1) over(order by RecordDate) as Temperature_2
from Weather
)
where Temperature > Temperature_2;
测试用例不通过,因为有一个测试用例是中间间隔了1天,并非前后2天,所以这道题不能通过偏移函数来进行求解,还是得通过前后2天进行连接,如果找不到对应的时间差则关联不上,这样的答案才是正确的。
在 MySQL 环境中,使用 join 和 datediff 函数进行求解。
---- MySQL ----
select a.Id as Id
from Weather a
left join Weather b
on datediff(a.RecordDate, b.RecordDate) = 1
where a.Temperature > b.Temperature; ---- 274ms
-- 第一次提交的时候把最后温度的过滤条件写成了and,怪不得提交不通过,改为where之后便可以了。
---- MySQL ----
# Write your MySQL query statement below
select a.Id
from Weather a,
Weather b
where a.Temperature > b.Temperature
and datediff(a.RecordDate, b.RecordDate) = 1; ---- 275ms
这样子就通过?得好好考虑一下。。
---- oracle ----
/* Write your PL/SQL query statement below */
select a.Id as Id
from Weather a
left join Weather b
on a.RecordDate = b.RecordDate - 1
where a.Temperature > b.Temperature;
---- 没通过
其实本身,这样子的解法是没有问题的,只是测试样例中的数据不够规范,所以测试才不通过。
另外,果然看到一种通过偏移函数解答的,再进行尝试一番,修改一下。
---- oracle ----
/* Write your PL/SQL query statement below */
select t.Id as Id
from
(
select Id,
RecordDate,
Temperature,
lag(Temperature,1) over(order by RecordDate) as Temperature_2,
lag(RecordDate,1) over(order by RecordDate) as RecordDate_2
from Weather
) t
where t.Temperature > t.Temperature_2
and round(to_number(t.RecordDate - t.RecordDate_2)) = 1; ---- 537ms
证明,偏移函数还可以可以的!!!
思考
复习一下 MySQL 的 datediff 函数。
第一个参数减掉第二个参数。
datediff('2007-12-31','2007-12-30'); # 1
datediff('2010-12-30','2010-12-31'); # -1
另外,也可以通过 date_add 函数进行时间加减。
date_add('2019-10-26', interval 1 day)
使用 oracle 中的偏移函数进行求解。
LeetCode:197.上升的温度的更多相关文章
- LeetCode 197. Rising Temperature (上升的温度)
题目标签: 题目给了我们一个 温度表格,让我们找到 所有温度比之前一天高的,返回id. 建立 Weather w1, Weather w2,找到当w1 的温度 大于 w2 的时候,而且 w1 的日期是 ...
- [LeetCode] Daily Temperatures 日常温度
Given a list of daily temperatures, produce a list that, for each day in the input, tells you how ma ...
- leetcode 197. Rising Temperature sql_Date用法
https://leetcode.com/problems/rising-temperature/description/ 题目需要选出今天比昨天气温高的ID 用join,默认是inner join需 ...
- LeetCode 739:每日温度 Daily Temperatures
题目: 根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高超过该日的天数.如果之后都不会升高,请在该位置用 0 来代替. 例如,给定一个列表 temperature ...
- Leetcode 197. Rising Temperature
Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to ...
- [LeetCode] 197. Rising Temperature_Easy tag: SQL
Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to ...
- leetcode题库
leetcode题库 #题名题解通过率难度出现频率 1 两数之和 46.5%简单2 两数相加 35.5%中等3 无重复字符的最长子串 31.1%中等4 寻找两个有序数组的中位 ...
- 【MySQL 基础】MySQ LeetCode
MySQL LeetCode 175. 组合两个表 题目描述 表1: Person +-------------+---------+ | 列名 | 类型 | +-------------+----- ...
- mysql学习 | LeetCode数据库简单查询练习
力扣:https://leetcode-cn.com/ 力扣网数据库练习:https://leetcode-cn.com/problemset/database/ 文章目录 175. 组合两个表 题解 ...
随机推荐
- 客户端配置代理服务实现yum上外网
vi /etc/profile http_proxy=http://172.20.188.193:3128/https_proxy=https://172.20.188.193:3128/expor ...
- 循环引擎 greenlet 没有显式调度的微线程,换言之 协程
小结: 1. micro-thread with no implicit scheduling; coroutines, in other words. 没有显式调度的微线程,换言之 协程 2. 一个 ...
- Swift 条件语句
条件语句通过设定的一个或多个条件来执行程序,在条件为真时执行指定的语句,在条件为 false 时执行另外指定的语句. 可以通过下图来简单了解条件语句的执行过程: Swift 提供了以下几种类型的条件语 ...
- 003-多线程-JUC集合-Set-CopyOnWriteArrayList
一.概述 它是线程安全的无序的集合,可以将它理解成线程安全的HashSet.有意思的是,CopyOnWriteArraySet和HashSet虽然都继承于共同的父类AbstractSet:但是,Has ...
- iOS优秀第三方框架
1.数据库操作:BGDB_OC 2.腾讯崩溃检测: Bugly 3.强大易用的bug收集管理工具: Bugtags 4.访问本地相册,选择多张图片: HMImagePicker 5.iOS颜色库: W ...
- cent7虚拟机切换root时出现"ABRT has detected ..."问题
今早打开虚拟机,在sudo -i的时候,发现出了这个问题: 于是我按照提示,执行abrt-cli list --since 1563382095后,结果如下: 大意就是内核出现了某CPU软死锁的问 ...
- C# lambda查询带返回值
问题来源: <深入理解C#(第3版)> 11页 具体如下: var lists=new List<string>{"111","222" ...
- VS2013:error C1069: 无法读取编译器命令行
前一阵搞python和matlab,没用VS 2013,今天打开一个C++程序想跑一跑,突然蹦出这么个错误,然后发现电脑上所有的程序都会这样了. 后来发现是TMP/TEMP环境变量路径有空格的问题,更 ...
- macos 更改罗技k810无线键盘的映射
在mac系统中,command键非常关键,但k810接入后, win键被映射为Command,而Alt的位置却是mac内置键盘的Command的位置. 为方便使用,可以把Win键和Alt键做一个对换. ...
- B8 Concurrent JDK中的乐观锁与原子类
[概述] 乐观锁采用的是一种无锁的思想,总是假设最好的情况,认为一个事务在读取数据的时候,不会有别的事务对数据进行修改,只需要在修改数据的时候判断原数据数据是否已经被修改了.JDK 中 java.ut ...