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

结果

解题步骤:

相关推荐
kkeeper~18 小时前
0基础C语言积跬步之数据在内存中的存储
c语言·数据结构·算法
wabs66619 小时前
关于贪心算法的一些自我总结【力扣45.跳跃游戏II】【灵感来源:代码随想录】
算法·贪心算法·复盘
2401_8769641319 小时前
【湖北专升本】2026湖北专升本真题PDF+备考资料汇总
数据结构·人工智能·经验分享·深度学习·算法·计算机视觉
嗝o゚20 小时前
CANN GE 算子融合——融合算法与调度策略
算法·昇腾·cann·ge
小江的记录本20 小时前
【JVM虚拟机】垃圾回收GC:垃圾回收算法:标记-清除、标记-复制、标记-整理、分代收集(附《思维导图》+《面试高频考点清单》)
java·jvm·后端·python·算法·安全·面试
Ulyanov21 小时前
用声明式语法重新定义Python桌面UI:QML+PySide6现代开发入门(一)
开发语言·python·算法·ui·系统仿真·雷达电子对抗仿真
数据科学小丫21 小时前
特征工程处理
人工智能·算法·机器学习
秦明月131 天前
电芯装配测试线安全回路设计实战
经验分享·其他·职场和发展·创业创新·学习方法
z落落1 天前
C#参数区别
java·算法·c#
c238561 天前
vector(下)
数据结构·算法