LeetCode刷题实战539:最小时间差
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(ListtimePoints ) {
int size=timePoints.size();
//仿时间戳做个今明两天的数组
int[] time=new int[size*2];
for (int i = 0,idx=0; i2,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;
}
}