steve-johnson-TnjzgonRFPU-unsplash.jpg
中等给定一个 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
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

代码

  1. /**
  2. * @param {string[]} timePoints
  3. * @return {number}
  4. */
  5. var findMinDifference = function (timePoints) {
  6. const arr = new Array();
  7. for (let i = 0; i < timePoints.length; i++) {
  8. arr.push(minutes(timePoints[i]));
  9. }
  10. arr.sort((prev, curt) => {
  11. return prev - curt;
  12. });
  13. let min = arr[1] - arr[0];
  14. for (let i = 1; i < arr.length; i++) {
  15. min = Math.min(min, arr[i] - arr[i - 1]);
  16. }
  17. return Math.min(min, arr[0] + 1440 - arr[arr.length - 1]);
  18. };
  19. const minutes = (time) => {
  20. const m = parseInt(time.split(":")[1]);
  21. const h = parseInt(time.split(":")[0]);
  22. return m + h * 60;
  23. };