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

结果

解题步骤:

相关推荐
学逆向的2 小时前
汇编——位运算
开发语言·汇编·算法·网络安全
可编程芯片开发2 小时前
基于simulink的PEM燃料电池控制系统建模与仿真,对比PID,积分分离以及滑模控制器
算法
学究天人3 小时前
数学公理体系大全:Comprehensive Collection of Mathematical Axiom Systems(卷2)
算法·数学建模·动态规划·图论·抽象代数·拓扑学
玛卡巴卡ldf3 小时前
【LeetCode 手撕算法】(细节知识点总结)
java·数据结构·算法·leetcode·力扣
wanzehongsheng4 小时前
零碳产业园光伏园区光伏电站追踪对比固定:发电增益技术边界分析
算法·光伏发电·光伏·零碳园区·太阳能追光·低碳环保·追踪电站
KobeSacre4 小时前
CyclicBarrier 源码
java·jvm·算法
手写码匠4 小时前
注意力机制全家桶:从 Multi-Head 到 GQA 再到 Flash Attention 的手写实现
人工智能·深度学习·算法·aigc
大鱼>4 小时前
模型公平性与偏差检测:AI伦理实战指南
人工智能·深度学习·算法·机器学习
学究天人5 小时前
数学公理体系大全:Comprehensive Collection of Mathematical Axiom Systems(卷3.2)
线性代数·算法·机器学习·数学建模·动态规划·抽象代数·拓扑学
tkevinjd5 小时前
力扣322-零钱兑换
算法·leetcode·动态规划