539. Minimum Time Difference

Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minutes difference between any two time points in the list.

Example 1:

Input: ["23:59","00:00"]
Output: 1

 Note:

  1. The number of time points in the given list is at least 2 and won't exceed 20000.
  2. The input time is legal and ranges from 00:00 to 23:59.

题目含义:给定一定24格式的Hour:Minutes字符,找到任意两个时间点的最小时间差

复制代码
 1     public int findMinDifference(List<String> timePoints) {
 2         boolean[] minutes = new boolean[24 * 60];
 3         for (String time : timePoints) {
 4             String[] values = time.split(":");
 5             int minute = Integer.valueOf(values[0]) * 60 + Integer.valueOf(values[1]);
 6             if (minutes[minute]) return 0;
 7             minutes[minute] = true;
 8         }
 9         int min = Integer.MAX_VALUE, left = Integer.MAX_VALUE, right = Integer.MIN_VALUE;
10         int previous = 0;//上一个时间值
11         for (int i = 0; i < minutes.length; i++) {
12             if (!minutes[i]) continue;
13             if (left != Integer.MAX_VALUE) {
14                 min = Math.min(min, i - previous);//min记录了最小的时间差
15             }
16             left = Math.min(left, i);//距离零点最近的点
17             right = Math.max(right, i);//距离零点最远的点
18             previous = i;
19         }
20         return Math.min(min, 24 * 60 - right + left);     
21     }
复制代码

 

posted @   daniel456  阅读(246)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示