【數學】C074_LC_時鐘指針的夾角(補角)

一、Problem

Given two numbers, hour and minutes. Return the smaller angle (in degrees) formed between the hour and the minute hand.

在這裏插入圖片描述

二、Solution

方法一:求補角

class Solution {
    public double angleClock(int h, int m) {
        double h2a = 360/12, m2a = h2a/60;	
        double h_angle = h2a*h + m2a*m;		//小時的貢獻
        double m_angle = m * 360/60;		//分鐘的貢獻
        double now = Math.abs(h_angle - 0 - m_angle - 0);
        double oth = 360 - now;				//補角
        return Math.min(now, oth);
    }
}

複雜度分析

  • 時間複雜度:O(1)O(1)
  • 空間複雜度:O(1)O(1)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章