539. 最小時間差private int parse(String str) { String[] times = str.split(":"); String ho

給定一個 24 小時制(小時:分鐘)的時間列表,找出列表中任意兩個時間的最小時間差並已分鐘數表示。


示例 1:

輸入: ["23:59","00:00"]
輸出: 1

備註:

列表中時間數在 2~20000 之間。
每個時間取值在 00:00~23:59 之間。

來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/minimum-time-difference
著作權歸領釦網絡所有。商業轉載請聯繫官方授權,非商業轉載請註明出處。

 

private int parse(String str) {
        String[] times = str.split(":");
        String hour = times[0];
        String minute = times[1];
        
        int t = (minute.charAt(0) - '0') * 10 + (minute.charAt(1) - '0');
        t += (60 * ((hour.charAt(0) - '0') * 10 + (hour.charAt(1) - '0')));
        return t;    
    }
    //入口
    public int findMinDifference(List<String> timePoints) {
        int len = timePoints.size();
        if (len < 2) {
            return 0;
        }
        int[] times = new int[len];
        for(int i = 0; i < len; i++) {
            times[i] = parse(timePoints.get(i));
        }
        
        Arrays.sort(times);
        int min = Integer.MAX_VALUE;
        for(int i = 0; i < len - 1; i++) {
            min = Math.min(min, times[i+1] - times[i]);
        }
        
        min = Math.min(min, 24 * 60 - times[len - 1] + times[0]);
        
        return min;
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章