1344. Angle Between Hands of a Clock

Lc-1344

1344. Angle Between Hands of a Clock

题目大意:

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

解题思路:

初始化常数:one_min_angle = 6,one_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)。

注意:

None

复杂度:

Time Coplexity: O(1)
Space Complexity: O(1)

Code示例:

class Solution {
    public double angleClock(int hour, int minutes) {
        int minAngle = 6;
        int hourAngle = 30;
        double minutesAngle =  minAngle * minutes;
        double hoursAngle = (hour % 12 + minutes / 60.0) * hourAngle;
        
        double diff = Math.abs(hoursAngle - minutesAngle);
        return Math.min(diff, 360-diff);
    }
}

你可能感兴趣的:(Math,leetcode,java)