[LeetCode] Rising Temperature 上升温度

Given a Weather table, write a SQL query to find all dates‘ Ids with higher temperature compared to its previous (yesterday‘s) dates.

+---------+------------+------------------+
| Id(INT) | Date(DATE) | Temperature(INT) |
+---------+------------+------------------+
|       1 | 2015-01-01 |               10 |
|       2 | 2015-01-02 |               25 |
|       3 | 2015-01-03 |               20 |
|       4 | 2015-01-04 |               30 |
+---------+------------+------------------+

For example, return the following Ids for the above Weather table:

+----+
| Id |
+----+
|  2 |
|  4 |
+----+

这道题给了我们一个Weather表,让我们找出比前一天温度高的Id,由于Id的排列未必是按顺序的,所以我们要找前一天就得根据日期来找,我们可以使用MySQL的函数Datadiff来计算两个日期的差值,我们的限制条件是温度高且日期差1,参见代码如下:

解法一:

SELECT w1.Id FROM Weather w1, Weather w2
WHERE w1.Temperature > w2.Temperature AND DATEDIFF(w1.Date, w2.Date) = 1;

下面这种解法我们使用了MySQL的TO_DAYS函数,用来将日期换算成天数,其余跟上面相同:

解法二:

SELECT w1.Id FROM Weather w1, Weather w2
WHERE w1.Temperature > w2.Temperature AND TO_DAYS(w1.Date) = TO_DAYS(w2.Date) + 1;

我们也可以使用Subdate函数,来实现日期减1,参见代码如下:

解法三:

SELECT w1.Id FROM Weather w1, Weather w2
WHERE w1.Temperature > w2.Temperature AND SUBDATE(w1.Date, 1) = w2.Date;

最后来一种完全不一样的解法,使用了两个变量pre_t和pre_d分别表示上一个温度和上一个日期,然后当前温度要大于上一温度,且日期差为1,满足上述两条件的话选出来为Id,否则为NULL,然后更新pre_t和pre_d为当前的值,最后选出的Id不为空即可:

解法四:

SELECT Id FROM (
SELECT CASE WHEN Temperature > @pre_t AND DATEDIFF(Date, @pre_d) = 1 THEN Id ELSE NULL END AS Id,
@pre_t := Temperature, @pre_d := Date
FROM Weather, (SELECT @pre_t := NULL, @pre_d := NULL) AS init ORDER BY Date ASC
) id WHERE Id IS NOT NULL;

参考资料:

https://leetcode.com/discuss/33641/two-solutions

https://leetcode.com/discuss/52370/my-simple-solution-using-inner-join

https://leetcode.com/discuss/86435/a-simple-straightforward-solution-and-its-very-fast

LeetCode All in One 题目讲解汇总(持续更新中...)

时间: 2024-10-07 20:52:09

[LeetCode] Rising Temperature 上升温度的相关文章

LeetCode - Rising Temperature

Description:Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to its previous (yesterday's) dates. 选出比前一天温度高的Id 使用自连接,to_days函数是返回从零年开始的总天数. (为何把=误写成<会Runtime Error啊 不应该是WrongAnser!) # Write your MySQL q

[LeetCode]Rising Temperature,解题报告

目录 目录 题目 思路 AC SQL 题目 Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to its previous (yesterday's) dates. Id(INT) Date(DATE) Temperature(INT) 1 2015-01-01 10 2 2015-01-02 25 3 2015-01-03 20 4 2015-01-

LeetCode:Rising Temperature - 上浮的温度

1.题目名称 Rising Temperature(上浮的温度) 2.题目地址 https://leetcode.com/problems/rising-temperature 3.题目内容 给出一组每日的气温数据,返回当日气温高于昨日气温的日期. 例如,Weather表中的数据如下: +---------+------------+------------------+ | Id(INT) | Date(DATE) | Temperature(INT) | +---------+-------

[LeetCode][SQL]Rising Temperature

https://leetcode.com/problems/rising-temperature/ Rising Temperature Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to its previous (yesterday's) dates. +---------+------------+------------------+ | I

leetcode数据库sql之Rising Temperature

leetcode原文引用: Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to its previous (yesterday's) dates. +---------+------------+------------------+ | Id(INT) | Date(DATE) | Temperature(INT) | +---------+---

【Leetcode】Rising Temperature

题目链接:https://leetcode.com/problems/rising-temperature/ 题目: Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to its previous (yesterday's) dates. +---+----+------+ | Id(INT) | Date(DATE) | Temperature(IN

LeetCode 197. Rising Temperature (上升的温度)

题目标签: 题目给了我们一个 温度表格,让我们找到 所有温度比之前一天高的,返回id. 建立 Weather w1, Weather w2,找到当w1 的温度 大于 w2 的时候,而且 w1 的日期是在w2 的后一天,返回id. Java Solution: Runtime:  338 ms, faster than 66 % Memory Usage: N/A 完成日期:06/01/2019 关键点:利用TO_DAYS 来比较日期. # Write your MySQL query state

leetcode:Rising Temperature

Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to its previous (yesterday's) dates. +---------+------------+------------------+ | Id(INT) | Date(DATE) | Temperature(INT) | +---------+------------+----

Leetcode 197. Rising Temperature

Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to its previous (yesterday's) dates. +---------+------------+------------------+ | Id(INT) | Date(DATE) | Temperature(INT) | +---------+------------+----