给定一个 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”
class Solution {
/*
先把二十四小时制转换为分钟制,13:59 = 13 * 60 + 59;
然后按从小到大排序
注意把最小值+1440(24*60)来处理最大值的情况
例如:[23:58,00:23]
*/
public int findMinDifference(List<String> timePoints) {
List<Integer> list = new ArrayList<Integer>();
int n = timePoints.size();
for(String s : timePoints){
int num = ((s.charAt(0)-'0') * 10 + (s.charAt(1)-'0')) * 60
+ (s.charAt(3)-'0') * 10 + (s.charAt(4)-'0');
list.add(num);
}
Collections.sort(list);
list.add(list.get(0)+1440);
int min = 1440;
for(int i = 1; i < list.size(); ++i){
min = Math.min(min,list.get(i)-list.get(i-1));
}
return min;
}
}