๐ฏ Daily LeetCode Challenge Day_14 - Angle between hands of a clock
Explore - LeetCode
LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore.
leetcode.com
๋ฌธ์
Given two numbers, hour and minutes. Return the smaller angle (in degrees) formed between the hour and the minute hand.
Example 1:
Input: hour = 12, minutes = 30 Output: 165
Example 2:
Input: hour = 3, minutes = 30 Output: 75
Example 3:
Input: hour = 3, minutes = 15 Output: 7.5
Example 4:
Input: hour = 4, minutes = 50 Output: 155
Example 5:
Input: hour = 12, minutes = 0 Output: 0
Constraints:
- 1 <= hour <= 12
- 0 <= minutes <= 59
- Answers within 10^-5 of the actual value will be accepted as correct.
๋ถ์นจ์ ๋ฐ๋ผ ์์นจ๋ ๋ฐ๋๋ค๋ ๊ฒ๊ณผ 360๋๋ 0๋๋ก ๋ฐ๊ฟ์ฃผ๋ ๊ฒ, ๋ ์นจ์ ์์ ๊ฐ์ ๋ฐํํด์ผ ํ๋๊ฒ๋ง ์๊ฐํด์ฃผ๋ฉด ๋๋ค.
class Solution {
func angleClock(_ hour: Int, _ minutes: Int) -> Double {
var mAngle : Double = 6*Double(minutes)
var hAngle : Double = ( (30*Double(hour)) + (30*Double(minutes)/60) )
if hAngle >= 360 { hAngle -= 360 }
var minAngle = min(abs(hAngle-mAngle),abs(mAngle-hAngle))
return min(abs(360-minAngle),minAngle)
}
}