LeetCode 题解工作台

时钟指针的夹角

给你两个数 hour 和 minutes 。请你返回在时钟上,由给定时间的时针和分针组成的较小角的角度(60 单位制)。 示例 1: 输入: hour = 12, minutes = 30 输出: 165 示例 2: 输入: hour = 3, minutes = 30 输出; 75 示例 3: 输…

category

1

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 数学·driven

bolt

答案摘要

时针每小时移动 30 度,每分钟移动 0.5 度。分针每分钟移动 6 度。如果指针之间的夹角大于 180 度,则取其与 360 度的差值,以确保获得最小的夹角。 时间复杂度 ,空间复杂度 。

Interview AiBox logo

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

试试 AiBox 面试助手arrow_forward
description

题目描述

给你两个数 hour 和 minutes 。请你返回在时钟上,由给定时间的时针和分针组成的较小角的角度(60 单位制)。

 

示例 1:

输入:hour = 12, minutes = 30
输出:165

示例 2:

输入:hour = 3, minutes = 30
输出;75

示例 3:

输入:hour = 3, minutes = 15
输出:7.5

示例 4:

输入:hour = 4, minutes = 50
输出:155

示例 5:

输入:hour = 12, minutes = 0
输出:0

 

提示:

  • 1 <= hour <= 12
  • 0 <= minutes <= 59
  • 与标准答案误差在 10^-5 以内的结果都被视为正确结果。
lightbulb

解题思路

方法一:数学

时针每小时移动 30 度,每分钟移动 0.5 度。分针每分钟移动 6 度。如果指针之间的夹角大于 180 度,则取其与 360 度的差值,以确保获得最小的夹角。

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

1
2
3
4
5
6
7
class Solution:
    def angleClock(self, hour: int, minutes: int) -> float:
        h = 30 * hour + 0.5 * minutes
        m = 6 * minutes
        diff = abs(h - m)
        return min(diff, 360 - diff)
speed

复杂度分析

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

面试官常问的追问

外企场景
  • question_mark

    Pay attention to how the minute hand shifts the hour hand slightly as time progresses.

  • question_mark

    Candidates should show awareness of the fact that the angle must be adjusted for the smaller value, ensuring the correct result is found.

  • question_mark

    Look for clarity in the explanation of the mechanics of both hands and how their positions affect the angle.

warning

常见陷阱

外企场景
  • error

    Forgetting to account for the minute hand's effect on the hour hand.

  • error

    Not adjusting the angle when it exceeds 180 degrees to ensure the smaller angle is returned.

  • error

    Misunderstanding how to convert time into angles, leading to incorrect calculations.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Return the larger angle instead of the smaller one.

  • arrow_right_alt

    Calculate the angle between the hands at a different scale, like a 24-hour format clock.

  • arrow_right_alt

    Consider a case where the input time is provided in 24-hour format and adjust the formula accordingly.

help

常见问题

外企场景

时钟指针的夹角题解:数学·driven | LeetCode #1344 中等