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

结果

解题步骤:

相关推荐
橘颂TA2 小时前
【Linux 网络】TCP 拥塞控制与异常处理:从原理到实践的深度剖析
linux·运维·网络·tcp/ip·算法·职场和发展·结构与算法
tobias.b3 小时前
408真题解析-2010-9-数据结构-折半查找的比较次数
java·数据结构·算法·计算机考研·408真题解析
源代码•宸3 小时前
Leetcode—404. 左叶子之和【简单】
经验分享·后端·算法·leetcode·职场和发展·golang·dfs
WBluuue3 小时前
数据结构与算法:dp优化——优化尝试和状态设计
c++·算法·leetcode·动态规划
im_AMBER3 小时前
Leetcode 105 K 个一组翻转链表
数据结构·学习·算法·leetcode·链表
sin_hielo3 小时前
leetcode 1877
数据结构·算法·leetcode
睡不醒的kun4 小时前
定长滑动窗口-基础篇(2)
数据结构·c++·算法·leetcode·职场和发展·滑动窗口·定长滑动窗口
庄小焱4 小时前
【机器学习】——房屋销售价格预测实战
人工智能·算法·机器学习·预测模型
txzrxz4 小时前
单调栈详解(含题目)
数据结构·c++·算法·前缀和·单调栈