0
点赞
收藏
分享

微信扫一扫

LeetCode 剑指 Offer II 035. 最小时间差

LeetCode 剑指 Offer II 035. 最小时间差

文章目录

题目描述

LeetCode 剑指 Offer II 035. 最小时间差
提示:


一、解题关键词


二、解题报告

1.思路分析

2.时间复杂度

3.代码示例

class Solution {

    public int findMinDifference(List<String> timePoints) {
        int n = timePoints.size();
        if (n > 1440) {
            return 0;
        }
        Collections.sort(timePoints);
        int ans = Integer.MAX_VALUE;
        int t0Minutes = getMinutes3(timePoints.get(0));
        int preMinutes = t0Minutes;
        for (int i = 1; i < n; ++i) {
            int minutes = getMinutes3(timePoints.get(i));
            ans = Math.min(ans, minutes - preMinutes); // 相邻时间的时间差
            preMinutes = minutes;
        }
        ans = Math.min(ans, t0Minutes + 1440 - preMinutes); // 首尾时间的时间差
        return ans;
    }

    public int getMinutes3(String t) {
        return ((t.charAt(0) - '0') * 10 + (t.charAt(1) - '0')) * 60 + (t.charAt(3) - '0') * 10 + (t.charAt(4) - '0');
    }
}

2.知识点

核心在于排序

总结

相同题目

539. 最小时间差

举报

相关推荐

0 条评论