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  │
└─────┘

结果

解题步骤:

相关推荐
灵感__idea12 小时前
Hello 算法:众里寻她千“百度”
前端·javascript·算法
Wect21 小时前
LeetCode 130. 被围绕的区域:两种解法详解(BFS/DFS)
前端·算法·typescript
NAGNIP1 天前
一文搞懂深度学习中的通用逼近定理!
人工智能·算法·面试
颜酱2 天前
单调栈:从模板到实战
javascript·后端·算法
CoovallyAIHub2 天前
仿生学突破:SILD模型如何让无人机在电力线迷宫中发现“隐形威胁”
深度学习·算法·计算机视觉
CoovallyAIHub2 天前
从春晚机器人到零样本革命:YOLO26-Pose姿态估计实战指南
深度学习·算法·计算机视觉
CoovallyAIHub2 天前
Le-DETR:省80%预训练数据,这个实时检测Transformer刷新SOTA|Georgia Tech & 北交大
深度学习·算法·计算机视觉
CoovallyAIHub2 天前
强化学习凭什么比监督学习更聪明?RL的“聪明”并非来自算法,而是因为它学会了“挑食”
深度学习·算法·计算机视觉
CoovallyAIHub2 天前
YOLO-IOD深度解析:打破实时增量目标检测的三重知识冲突
深度学习·算法·计算机视觉
NAGNIP2 天前
轻松搞懂全连接神经网络结构!
人工智能·算法·面试