​LeetCode刷题实战197:上升的温度

程序IT圈

共 1213字,需浏览 3分钟

 · 2021-03-01

算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !

今天和大家聊的问题叫做 上升的温度,我们先来看题面:
https://leetcode-cn.com/problems/rising-temperature/

Write an SQL query to find all dates' id with higher temperature compared to its previous dates (yesterday).


Return the result table in any order.

题意


编写一个 SQL 查询,来查找与之前(昨天的)日期相比温度更高的所有日期的 id 。返回结果 不要求顺序 。

解题


首先我们找到每一天的前一天,然后判断温度是否升高了,筛选出温度上升的id即可。

select w1.Id as Id
from Weather w1
#连接Weather表(自连接)
inner join Weather w2
#连接条件,w2是w1的前一天
on datediff(w1.RecordDate, w2.RecordDate) = 1
#筛选条件:温度升高
where w1.Temperature > w2.Temperature;



好了,今天的文章就到这里,如果觉得有所收获,请顺手点个在看或者转发吧,你们的支持是我最大的动力 。

上期推文:

LeetCode1-180题汇总,希望对你有点帮助!
LeetCode刷题实战181:超过经理收入的员工
LeetCode刷题实战182:查找重复的电子邮箱
LeetCode刷题实战183:从不订购的客户
LeetCode刷题实战184:部门工资最高的员工
LeetCode刷题实战185:部门工资前三高的所有员工
LeetCode刷题实战186:翻转字符串里的单词 II
LeetCode刷题实战187:重复的DNA序列
LeetCode刷题实战188:买卖股票的最佳时机 IV
LeetCode刷题实战189:旋转数组
LeetCode刷题实战190:颠倒二进制位
LeetCode刷题实战191:位1的个数
LeetCode刷题实战192:统计词频
LeetCode刷题实战193:有效电话号码
LeetCode刷题实战194:转置文件
LeetCode刷题实战195:第十行
LeetCode刷题实战196:删除重复的电子邮箱

浏览 15
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报