Given a list of 24-hour clock time points in “HH:MM” format, return the minimum minutes difference between any two time-points in the list.
示例
示例 1:
输入:timePoints = ["23:59","00:00"]
输出:1
示例 2:
输入:timePoints = ["00:00","23:59","00:00"]
输出:0
解题
class Solution {
public int findMinDifference(List<String> timePoints) {
int size=timePoints.size();
//仿时间戳做个今明两天的数组
int[] time=new int[size*2];
for (int i = 0,idx=0; i <size; idx=idx+2,i++) {
String[] split = timePoints.get(i).split(":");
int h = Integer.parseInt(split[0]);
int m = Integer.parseInt(split[1]);
//计算当前时间的时间戳
time[idx]=h*60+m;
//计算后一天当前时间的时间戳
//60*24=1440分钟
time[idx+1]=time[idx]+1440;
}
Arrays.sort(time);
int res=Integer.MAX_VALUE;
for (int i = 0; i+1 < time.length; i++) {
res=Math.min((time[i+1]-time[i]),res);
}
return res;
}
}
本篇文章来源于微信公众号:程序IT圈
原创文章,作者:栈长,如若转载,请注明出处:https://www.cxyquan.com/20049.html