The hour and minute hands move at different rates. By calculating their positions in degrees, we can find the absolute difference and ensure we return the smaller angle (≤180°). For example, at 3:15, the hour hand is at 97.5° and the minute hand at 90°, so the angle is 7.5°.
classSolution {
publicdoubleangleClock(int hour, int minutes) {
double ha = (hour % 12) * 30 + minutes * 0.5;
double ma = minutes * 6;
double ans = Math.abs(ha - ma);
return ans > 180 ? 360 - ans : ans;
}
}
1
2
3
4
5
6
7
8
classSolution {
funangleClock(hour: Int, minutes: Int): Double {
val ha = (hour % 12) * 30 + minutes * 0.5val ma = minutes * 6.0val ans = kotlin.math.abs(ha - ma)
returnif (ans > 180) 360 - ans else ans
}
}
1
2
3
4
5
6
classSolution:
defangleClock(self, hour: int, minutes: int) -> float:
ha: float = (hour %12) *30+ minutes *0.5 ma: float = minutes *6 ans: float = abs(ha - ma)
return360- ans if ans >180else ans
1
2
3
4
5
6
7
8
9
10
11
impl Solution {
pubfnangle_clock(hour: i32, minutes: i32) -> f64 {
let ha = (hour %12) asf64*30.0+ minutes asf64*0.5;
let ma = minutes asf64*6.0;
letmut ans = (ha - ma).abs();
if ans >180.0 {
ans =360.0- ans;
}
ans
}
}