1344-时钟指针的夹角

Raphael Liu Lv10

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

示例 1:

![](https://assets.leetcode-cn.com/aliyun-lc-
upload/uploads/2020/02/08/sample_1_1673.png)

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

示例 2:

![](https://assets.leetcode-cn.com/aliyun-lc-
upload/uploads/2020/02/08/sample_2_1673.png)

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

示例 3:

![](https://assets.leetcode-cn.com/aliyun-lc-
upload/uploads/2020/02/08/sample_3_1673.png)

**输入:** 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 以内的结果都被视为正确结果。

方法一:数学

其思想是分别计算 0 点垂线与每个指针之间的角度。答案是这两个角度的差。

img_v2_e1eea588-d1d5-483d-9c1b-50b16b8850bg.jpg{:width=400}

分针的角度:
我们从分针开始,整个圆 360° 有 60 分钟。分针指针移动一分钟的角度是 1 \text{ min} = 360° / 60 = 6°。

img_v2_9f2f6819-5159-4534-ac8c-cc70ae4d5efg.jpg{:width=400}

现在可以很容易地找到 0 点垂直线和分钟指针之间的角度:minutes_angle} = \text{minutes} \times 6°。

img_v2_6cfe2d6d-7cd1-40f7-b95e-7c7830658bcg.jpg{:width=400}

时针的角度:
与分针的角度相似,整个圆 360° 有 12 个小时,因此每个小时 1 \text{h} = 360° / 12 = 30°。

img_v2_8b49ce8e-a7d9-4470-99eb-55fffd2ef66g.jpg{:width=400}

则时针的角度为:hour_angle} = \text{hour} \times 30°。

img_v2_86787012-86eb-4a87-85dc-3faf1e1e4b9g.jpg{:width=400}

由于 12 点的角度实际为 0,则需要修改表达式为:hour_angle} = (\text{hour mod } 12) \ \times 30°。

在分钟指针大于 0 的情况下,必须考虑到时针指针额外的移动:它不在整数值之间跳跃,是跟着分针移动。

\text{hour_angle} = \left(\text{hour mod } 12 + \text{minutes} / 60 \right)\times 30°

img_v2_f38f2aef-87ab-4e82-9b97-352a41d2d9cg.jpg{:width=400}

算法:

  • 初始化常数:one_min_angle = 6one_hour_angle = 30
  • 分针指针与 0 点垂线的角度为:minutes_angle = one_min_angle * minutes
  • 时针指针与 0 点垂线的角度为:hour_angle = (hour % 12 + minutes / 60) * one_hour_angle
  • 得到差:diff = abs(hour_angle - minutes_angle)
  • 返回最小的角度:min(diff, 360 - diff)
[solution1-Python]
1
2
3
4
5
6
7
8
9
10
class Solution:
def angleClock(self, hour: int, minutes: int) -> float:
one_min_angle = 6
one_hour_angle = 30

minutes_angle = one_min_angle * minutes
hour_angle = (hour % 12 + minutes / 60) * one_hour_angle

diff = abs(hour_angle - minutes_angle)
return min(diff, 360 - diff)
[solution1-Java]
1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public double angleClock(int hour, int minutes) {
int oneMinAngle = 6;
int oneHourAngle = 30;

double minutesAngle = oneMinAngle * minutes;
double hourAngle = (hour % 12 + minutes / 60.0) * oneHourAngle;

double diff = Math.abs(hourAngle - minutesAngle);
return Math.min(diff, 360 - diff);
}
}
[solution1-C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
double angleClock(int hour, int minutes) {
int oneMinAngle = 6;
int oneHourAngle = 30;

double minutesAngle = oneMinAngle * minutes;
double hourAngle = (hour % 12 + minutes / 60.0) * oneHourAngle;

double diff = abs(hourAngle - minutesAngle);
return min(diff, 360 - diff);
}
};
[solution1-Go]
1
2
3
4
5
6
7
8
9
10
11
func angleClock(hour int, minutes int) float64 {
var oneMinAngle, oneHourAngle, minutesAngle, hourAngle, diff float64;
oneMinAngle = 6;
oneHourAngle = 30;

minutesAngle = oneMinAngle * float64(minutes);
hourAngle = (float64(hour % 12) + float64(minutes) / 60.0) * oneHourAngle;

diff = math.Abs(hourAngle - minutesAngle);
return math.Min(diff, 360 - diff);
}

复杂度分析

  • 时间复杂度:\mathcal{O}(1)。
  • 空间复杂度:\mathcal{O}(1)。
 Comments
On this page
1344-时钟指针的夹角