offer2-35


给定一个 24 小时制(小时:分钟 **”HH:MM”**)的时间列表,找出列表中任意两个时间的最小时间差并以分钟数表示。

示例 1:

输入:timePoints = ["23:59","00:00"]
输出:1

示例 2:

输入:timePoints = ["00:00","23:59","00:00"]
输出:0

提示:

  • 2 <= timePoints <= 2 * 104
  • timePoints[i] 格式为 “HH:MM”

//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int findMinDifference(List<String> timePoints) {
        List<Integer> timesList = new ArrayList<>();
        for (String time : timePoints) {
            timesList.add(convertTime(time));
        }
        Collections.sort(timesList);
        int result = 0;

        for (int i = 0; i < timesList.size(); i++) {
            if (i == 0) {
                result =  timesList.get(0) + 60 * 24 - timesList.get(timesList.size() - 1);
            } else if (timesList.get(i) - timesList.get(i - 1) < result) {
                result = timesList.get(i) - timesList.get(i - 1);
            }
        }

        return result;
    }

    private int convertTime(String timePoint) {
        String[] times = timePoint.split(":");
        return Integer.valueOf(times[0]) * 60 +  Integer.valueOf(times[1]);
    }
}
//leetcode submit region end(Prohibit modification and deletion)

文章作者: 倪春恩
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 倪春恩 !