中等给定一个 24 小时制(小时:分钟 “HH:MM”)的时间列表,找出列表中任意两个时间的最小时间差并以分钟数表示。
示例 1:
输入:timePoints = [“23:59”,”00:00”]
输出:1
示例 2:
输入:timePoints = [“00:00”,”23:59”,”00:00”]
输出:0
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/minimum-time-difference
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码
/**
* @param {string[]} timePoints
* @return {number}
*/
var findMinDifference = function (timePoints) {
const arr = new Array();
for (let i = 0; i < timePoints.length; i++) {
arr.push(minutes(timePoints[i]));
}
arr.sort((prev, curt) => {
return prev - curt;
});
let min = arr[1] - arr[0];
for (let i = 1; i < arr.length; i++) {
min = Math.min(min, arr[i] - arr[i - 1]);
}
return Math.min(min, arr[0] + 1440 - arr[arr.length - 1]);
};
const minutes = (time) => {
const m = parseInt(time.split(":")[1]);
const h = parseInt(time.split(":")[0]);
return m + h * 60;
};