Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minutes difference between any two time points in the list.

Example 1:

Input: ["23:59","00:00"]
Output: 1

Note:

  1. The number of time points in the given list is at least 2 and won't exceed 20000.
  2. The input time is legal and ranges from 00:00 to 23:59.

这道题给了我们一系列无序的时间点,让我们求最短的两个时间点之间的差值。那么最简单直接的办法就是给数组排序,这样时间点小的就在前面了,然后我们分别把小时和分钟提取出来,计算差值,注意唯一的特殊情况就是第一个和末尾的时间点进行比较,第一个时间点需要加上24小时再做差值,参见代码如下:

解法一:

class Solution {
public:
int findMinDifference(vector<string>& timePoints) {
int res = INT_MAX, n = timePoints.size(), diff = ;
sort(timePoints.begin(), timePoints.end());
for (int i = ; i < n; ++i) {
string t1 = timePoints[i], t2 = timePoints[(i + ) % n];
int h1 = (t1[] - '') * + t1[] - '';
int m1 = (t1[] - '') * + t1[] - '';
int h2 = (t2[] - '') * + t2[] - '';
int m2 = (t2[] - '') * + t2[] - '';
diff = (h2 - h1) * + (m2 - m1);
if (i == n - ) diff += * ;
res = min(res, diff);
}
return res;
}
};

下面这种写法跟上面的大体思路一样,写法上略有不同,是在一开始就把小时和分钟数提取出来并计算总分钟数存入一个新数组,然后再对新数组进行排序,再计算两两之差,最后还是要处理首尾之差,参见代码如下:

解法二:

class Solution {
public:
int findMinDifference(vector<string>& timePoints) {
int res = INT_MAX, n = timePoints.size();
vector<int> nums;
for (string str : timePoints) {
int h = stoi(str.substr(, )), m = stoi(str.substr());
nums.push_back(h * + m);
}
sort(nums.begin(), nums.end());
for (int i = ; i < n; ++i) {
res = min(res, nums[i] - nums[i - ]);
}
return min(res, + nums[] - nums.back());
}
};

上面两种方法的时间复杂度都是O(nlgn),我们来看一种O(n)时间复杂度的方法,由于时间点并不是无穷多个,而是只有1440个,所以我们建立一个大小为1440的数组来标记某个时间点是否出现过,如果之前已经出现过,说明有两个相同的时间点,直接返回0即可;若没有,将当前时间点标记为出现过。我们还需要一些辅助变量,pre表示之前遍历到的时间点,first表示按顺序排的第一个时间点,last表示按顺序排的最后一个时间点,然后我们再遍历这个mask数组,如果当前时间点出现过,再看如果first不为初始值的话,说明pre已经被更新过了,我们用当前时间点减去pre来更新结果res,然后再分别更新first,last,和pre即可,参见代码如下:

解法三:

class Solution {
public:
int findMinDifference(vector<string>& timePoints) {
int res = INT_MAX, pre = , first = INT_MAX, last = INT_MIN;
vector<int> mask(, );
for (string str : timePoints) {
int h = stoi(str.substr(, )), m = stoi(str.substr());
if (mask[h * + m] == ) return ;
mask[h * + m] = ;
}
for (int i = ; i < ; ++i) {
if (mask[i] == ) {
if (first != INT_MAX) {
res = min(res, i - pre);
}
first = min(first, i);
last = max(last, i);
pre = i;
}
}
return min(res, + first - last);
}
};

参考资料:

https://discuss.leetcode.com/topic/83250/easy-c-solution

https://discuss.leetcode.com/topic/82573/verbose-java-solution-bucket

https://discuss.leetcode.com/topic/82575/java-o-nlog-n-o-n-time-o-1-space-solutions

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Minimum Time Difference 最短时间差的更多相关文章

  1. [LeetCode] Minimum Absolute Difference in BST 二叉搜索树的最小绝对差

    Given a binary search tree with non-negative values, find the minimum absolute difference between va ...

  2. LeetCode Minimum Time Difference

    原题链接在这里:https://leetcode.com/problems/minimum-time-difference/description/ 题目: Given a list of 24-ho ...

  3. LeetCode Minimum Absolute Difference in BST

    原题链接在这里:https://leetcode.com/problems/minimum-absolute-difference-in-bst/#/description 题目: Given a b ...

  4. 539 Minimum Time Difference 最小时间差

    给定一个 24 小时制(小时:分钟)的时间列表,找出列表中任意两个时间的最小时间差并已分钟数表示.示例 1:输入: ["23:59","00:00"]输出: 1 ...

  5. 【LeetCode】539. Minimum Time Difference 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/minimum-t ...

  6. [Swift]LeetCode539. 最小时间差 | Minimum Time Difference

    Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minut ...

  7. LeetCode 530. Minimum Absolute Difference in BST (二叉搜索树中最小绝对差)

    Given a binary search tree with non-negative values, find the minimum absolute difference between va ...

  8. [Leetcode Week10]Minimum Time Difference

    Minimum Time Difference 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/minimum-time-difference/desc ...

  9. 【LeetCode】1200. Minimum Absolute Difference 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcode ...

随机推荐

  1. 使用 win10 的正确姿势 (第二版)

    文章为本人原创,转载请注明出处,谢谢. 17年9月初,写了第一篇<使用 win10 的正确姿势>,而现在半年多过去,文章更新了一些,主要是桌面的变化. 一. 重新定义桌面 我的桌面: 将桌 ...

  2. 基于FPGA的Cordic算法实现

    CORDIC(Coordinate Rotation Digital Computer)算法即坐标旋转数字计算方法,是J.D.Volder1于1959年首次提出,主要用于三角函数.双曲线.指数.对数的 ...

  3. Bate敏捷冲刺每日报告--day2

    1 团队介绍 团队组成: PM:齐爽爽(258) 小组成员:马帅(248),何健(267),蔡凯峰(285)  Git链接:https://github.com/WHUSE2017/C-team 2 ...

  4. Alpha冲刺Day8

    Alpha冲刺Day8 一:站立式会议 今日安排: 经过为期5天的冲刺,基本完成企业人员模块的开发.因第三方机构与企业存在委托的关系.第三方人员对于风险的自查.风险列表的展示以及自查风险的统计展示(包 ...

  5. python 闭包计算移动均值及nonlocal的使用

    class Averager1(): '''计算移动平均值的类第一种写法''' def __init__(self): self.series = [] def __call__(self,new_v ...

  6. python实现京东秒杀

    # _*_coding:utf-8_*_ from selenium import webdriver import datetime import time driver = webdriver.C ...

  7. Log4j详细教程

    一.入门实例 1.新建一个JAva工程,导入包log4j-1.2.17.jar,整个工程最终目录如下 2.src同级创建并设置log4j.properties ### 设置### log4j.root ...

  8. nyoj 第几是谁

    第几是谁? 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 现在有"abcdefghijkl"12个字符,将其按字典序排列,如果给出任意一种排列, ...

  9. php的数组的函数

    1.可以将一个二位数组转化成两个一维数组,没有指定键就是默认的索引 注意二位数组有几种类型,其中最常见的一种是外层循环是一个索引数组,然后内层是一个关联数组.这种通过便利第一层,然后第二层指定关联词就 ...

  10. Java.nio-随机读写汉字

    笔者最近在用多线程来计算中文文本的标点符号数目,遇到了以下问题: 在Windows下,文本中汉字通常采用Unicode编码,这就导致需要随机(RandomAccessFile)读取文本时,产生乱码现象 ...