日期:2026年6月18日
LeetCode 題目連結:1344. Angle Between Hands of a Clock
解題想法
中等難度,ZeroJudge 上有一題幾乎一樣的題目 d095. 00579 - ClockHands。解題時分成 3 個步驟:
- 計算時針與 12 時的夾角 $a$,先將 hour 對 12 取餘數,每小時轉 30 度。
- 計算分針與 12 時的夾角 $b$,每分鐘轉 6 度。
- 計算 $a, b$ 差值的絕對值 $c$,如果 $c$ 是鈍角,換成從另一側算角度。
Python 程式碼
Runtime: 0 ms, beats 100.00%. Memory: 19.37 MB, beats 84.16%.
class Solution:
def angleClock(self, hour: int, minutes: int) -> float:
a = (hour % 12 + minutes / 60.0) * 30.0 # 12 時變為 0 時,每小時轉 30 度
b = minutes * 6.0 # 每分鐘轉 6 度
c = abs(a - b) # 夾角
if c > 180.0: c = 360.0 - c # 如果是鈍角,換成從另一側算角度
return c