197. 上升的温度 - 力扣(LeetCode)

MySQL

sql 复制代码
# Write your MySQL query statement below
SELECT w1.id
FROM Weather w1, Weather w2
WHERE DATEDIFF(w1.recordDate, w2.recordDate) = 1 and w1.temperature > w2.temperature;
复制代码
DATEDIFF(date1, date2)
  • 返回的是 date1 - date2 的天数差(只考虑日期部分,忽略时间)。
  • 结果是一个整数
    • 正数:date1date2 之后
    • 负数:date1date2 之前
    • 0:两个日期相同

结果

解题步骤:

Pandas

python 复制代码
import pandas as pd

def rising_temperature(weather: pd.DataFrame) -> pd.DataFrame:
    # 确保 recordDate 是 datetime 类型
    weather['recordDate'] = pd.to_datetime(weather['recordDate'])
    
    # 给原表添加一列:表示"如果这是昨天,那么今天是哪天"
    weather_yesterday = weather.copy()
    weather_yesterday['next_day'] = weather_yesterday['recordDate'] + pd.Timedelta(days=1)
    
    # 将"今天"的表和"昨天"的表按日期连接:today.recordDate == yesterday.next_day
    merged = pd.merge(
        weather, 
        weather_yesterday[['next_day', 'temperature']], 
        left_on='recordDate', 
        right_on='next_day', 
        how='inner'
    )
    
    # 筛选今天温度 > 昨天温度的记录
    result = merged[merged['temperature_x'] > merged['temperature_y']]
    
    # 返回 id 列,并重命名为符合要求的格式(只保留 id)
    return result[['id']].rename(columns={'id': 'id'})
    

步骤1:日期格式转换

复制代码
weather['recordDate'] = pd.to_datetime(weather['recordDate'])
  • recordDate 列转换为 pandas 的 datetime 类型

  • 这样才能进行日期加减运算

步骤2:创建"昨天"数据表

复制代码
weather_yesterday = weather.copy()
weather_yesterday['next_day'] = weather_yesterday['recordDate'] + pd.Timedelta(days=1)
  • 复制原始数据,创建 weather_yesterday 副本

  • 添加新列 next_day = 原日期 + 1天

  • 逻辑:如果某行是昨天的记录,那么它的 next_day 就是今天

示例

复制代码
原始数据:
id | recordDate  | temperature
1  | 2023-01-01 | 25
2  | 2023-01-02 | 28  # 假设这是今天

处理后:
id | recordDate  | temperature | next_day
1  | 2023-01-01 | 25          | 2023-01-02  # 昨天记录,next_day是今天

步骤3:合并两张表

复制代码
merged = pd.merge(
    weather,  # 左表:今天的记录
    weather_yesterday[['next_day', 'temperature']],  # 右表:只保留需要的列
    left_on='recordDate',    # 左表的连接键:今天的日期
    right_on='next_day',     # 右表的连接键:next_day(即今天)
    how='inner'              # 内连接:只保留匹配成功的行
)
  • 逻辑:找到"今天"的记录和它对应的"昨天"的记录

  • 连接条件今天的日期 = 昨天的日期 + 1天

连接结果示例

复制代码
左表(今天)              右表(昨天)
id | recordDate  | temp   next_day  | yesterday_temp
2  | 2023-01-02 | 28   +  2023-01-02| 25
                    ↓
连接后:
id | recordDate  | temp_x | next_day  | temp_y
2  | 2023-01-02 | 28     | 2023-01-02| 25
# temp_x: 今天的温度(28)
# temp_y: 昨天的温度(25)

步骤4:筛选温度上升的记录

复制代码
result = merged[merged['temperature_x'] > merged['temperature_y']]
  • temperature_x: 今天的温度(来自左表)

  • temperature_y: 昨天的温度(来自右表)

  • 条件:今天温度 > 昨天温度

步骤5:返回结果

复制代码
return result[['id']].rename(columns={'id': 'id'})
  • 只返回 id

  • 重命名列(其实不变,为了格式一致性)

可视化流程

复制代码
原始数据:
┌────┬─────────────┬─────────────┐
│ id │ recordDate  │ temperature │
├────┼─────────────┼─────────────┤
│ 1  │ 2023-01-01 │     25      │ ← 昨天
│ 2  │ 2023-01-02 │     28      │ ← 今天
│ 3  │ 2023-01-03 │     26      │ ← 明天
└────┴─────────────┴─────────────┘

步骤1-2:创建昨天表
昨天表添加 next_day:
┌────┬─────────────┬─────────────┬─────────────┐
│ id │ recordDate  │ temperature │  next_day   │
├────┼─────────────┼─────────────┼─────────────┤
│ 1  │ 2023-01-01 │     25      │ 2023-01-02  │ ← next_day=今天
└────┴─────────────┴─────────────┴─────────────┘

步骤3:连接
匹配条件:今天.recordDate = 昨天.next_day
连接后:
┌─────┬─────────────┬───────────────┬─────────────┬───────────────┐
│ id  │ recordDate  │ temperature_x │  next_day   │ temperature_y │
├─────┼─────────────┼───────────────┼─────────────┼───────────────┤
│  2  │ 2023-01-02 │      28       │ 2023-01-02  │      25       │
└─────┴─────────────┴───────────────┴─────────────┴───────────────┘

步骤4:筛选
28 (今天) > 25 (昨天) ✓ → 保留

步骤5:返回
┌─────┐
│ id  │
├─────┤
│  2  │
└─────┘

结果

解题步骤:

相关推荐
_OP_CHEN2 小时前
【算法基础篇】(五十七)线性代数之矩阵乘法从入门到实战:手撕模板 + 真题详解
线性代数·算法·矩阵·蓝桥杯·c/c++·矩阵乘法·acm/icpc
天天爱吃肉82182 小时前
【跨界封神|周杰伦×王传福(陶晶莹主持):音乐创作与新能源NVH测试,底层逻辑竟完全同源!(新人必看入行指南)】
python·嵌入式硬件·算法·汽车
im_AMBER2 小时前
Leetcode 114 链表中的下一个更大节点 | 删除排序链表中的重复元素 II
算法·leetcode
xhbaitxl2 小时前
算法学习day38-动态规划
学习·算法·动态规划
多恩Stone2 小时前
【3D AICG 系列-6】OmniPart 训练流程梳理
人工智能·pytorch·算法·3d·aigc
历程里程碑2 小时前
普通数组----轮转数组
java·数据结构·c++·算法·spring·leetcode·eclipse
pp起床2 小时前
贪心算法 | part02
算法·leetcode·贪心算法
sin_hielo2 小时前
leetcode 1653
数据结构·算法·leetcode
2501_901147832 小时前
面试必看:优势洗牌
笔记·学习·算法·面试·职场和发展
YuTaoShao3 小时前
【LeetCode 每日一题】3634. 使数组平衡的最少移除数目——(解法二)排序 + 二分查找
数据结构·算法·leetcode