LeetCode 题解工作台

计算列车到站时间

给你一个正整数 arrivalTime 表示列车正点到站的时间(单位:小时),另给你一个正整数 delayedTime 表示列车延误的小时数。 返回列车实际到站的时间。 注意,该问题中的时间采用 24 小时制。 示例 1: 输入: arrivalTime = 15, delayedTime = 5 …

category

1

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

简单 · 数学·driven

bolt

答案摘要

我们直接计算列车实际到站的时间,即为 $arrivalTime + delayedTime$,但是由于时间采用 24 小时制,所以我们需要对结果取模,即 $(arrivalTime + delayedTime) \bmod 24$。 时间复杂度 ,空间复杂度 。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数学·driven 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个正整数 arrivalTime 表示列车正点到站的时间(单位:小时),另给你一个正整数 delayedTime 表示列车延误的小时数。

返回列车实际到站的时间。

注意,该问题中的时间采用 24 小时制。

 

示例 1:

输入:arrivalTime = 15, delayedTime = 5 
输出:20 
解释:列车正点到站时间是 15:00 ,延误 5 小时,所以列车实际到站的时间是 15 + 5 = 20(20:00)。

示例 2:

输入:arrivalTime = 13, delayedTime = 11
输出:0
解释:列车正点到站时间是 13:00 ,延误 11 小时,所以列车实际到站的时间是 13 + 11 = 24(在 24 小时制中表示为 00:00 ,所以返回 0)。

 

提示:

  • 1 <= arrivaltime < 24
  • 1 <= delayedTime <= 24
lightbulb

解题思路

方法一:数学

我们直接计算列车实际到站的时间,即为 arrivalTime+delayedTimearrivalTime + delayedTime,但是由于时间采用 24 小时制,所以我们需要对结果取模,即 (arrivalTime+delayedTime)mod24(arrivalTime + delayedTime) \bmod 24

时间复杂度 O(1)O(1),空间复杂度 O(1)O(1)

1
2
3
4
class Solution:
    def findDelayedArrivalTime(self, arrivalTime: int, delayedTime: int) -> int:
        return (arrivalTime + delayedTime) % 24
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Check if the candidate correctly uses modulo arithmetic when handling times that exceed 24.

  • question_mark

    Look for a clear understanding of the 24-hour clock system and time wrapping.

  • question_mark

    Test the candidate's ability to use basic math operations to solve the problem efficiently.

warning

常见陷阱

外企场景
  • error

    Failing to handle the wrapping correctly when the sum exceeds 24 hours.

  • error

    Not using modulo arithmetic to adjust the time.

  • error

    Returning incorrect results for times that wrap around past midnight, such as 23 + 3 = 2.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Changing the format of input times, e.g., from 24-hour format to 12-hour format.

  • arrow_right_alt

    Introducing multiple delays or considering days of the week.

  • arrow_right_alt

    Handling larger time ranges or applying delays greater than 24 hours.

help

常见问题

外企场景

计算列车到站时间题解:数学·driven | LeetCode #2651 简单