Featured image of post Angle Between Hands of Clock

Angle Between Hands of Clock

1344. 时钟指针的夹角

分析

  1. 在一个圆形时钟上:

    • 分针每分钟转动 360° ÷ 60
    • 时针每小时转动 30°360° ÷ 12),并且每分钟还会向前转动 0.5°30° ÷ 60min),即 minutes * 0.5°
  2. 因此可以计算:

    • 分针的角度为:a = minutes * 6
    • 时针的角度为:b = hour * 30 + minutes * 0.5
  3. 两者的夹角是 |a - b|,但由于时钟是圆形,返回较小的那个角度 std::min(std::abs(a - b), 360 - std::abs(a - b))

时间复杂度

时间复杂度 O(1)

空间复杂度

空间复杂度为 O(1)

C++代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution
{
public:
    double angleClock(int hour, int minutes)
    {
        // 将 hour 统一为 12 小时制
        hour %= 12;

        // 分针的角度(每分钟 6 度)
        double a = minutes * 6;

        // 时针的角度(每小时 30 度 + 每分钟 0.5 度)
        double b = hour * 30 + minutes * 0.5;

        // 返回两个角度的较小值
        return std::min(std::abs(a - b), 360 - std::abs(a - b));
    }
};
Built with Hugo
Theme Stack designed by Jimmy