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.

比较笨的一种方法就是我的方法,每次转成分钟然后做差就行了,注意要算补数求最小,计算时头和尾也要计算一次差值。

Runtime: 24 ms, faster than 25.52% of C++ online submissions for Minimum Time Difference.

class Solution {
public:
int minutediff(string time1, string time2){
int hour1 = atoi(time1.substr(,).c_str());
int hour2 = atoi(time2.substr(,).c_str());
int minute1 = atoi(time1.substr(,).c_str());
int minute2 = atoi(time2.substr(,).c_str());
minute1 = hour1 * + minute1;
minute2 = hour2 * + minute2;
int dif1 = abs(minute1 - minute2);
int dif2 = abs( * - dif1);
return min(dif1, dif2);
} int findMinDifference(vector<string>& timePoints) {
sort(timePoints.begin(), timePoints.end());
vector<int> timedif(timePoints.size(),INT_MAX);
for(int i=; i<timePoints.size(); i++){
timedif[i] = minutediff(timePoints[i],timePoints[i-]);
}
timedif[] = minutediff(timePoints[], timePoints.back());
int ret = *min_element(timedif.begin(),timedif.end());
return ret;
}
};

这一种做法时间不快因为每一次时间需要进行两次转换,下面看怎么进行一次转换。

另一种高级的做法,因为总共就24*60种可能,不如开一个24*60的数组,计算差值,妙!

class Solution {
public:
int findMinDifference(vector<string>& timePoints) {
vector<int> v( * , );
int r = INT_MAX, begin = * , last = - * ;
for (string & p : timePoints) {
int t = stoi(p.substr(, )) * + stoi(p.substr(, ));
if (v[t] == ) return ;
v[t] = ;
}
for (int i = ; i < * ; i++) {
if (v[i]) {
r = min(r, i - last);
begin = min(begin, i);
last = i;
}
}
return min(r, begin + * - last);
}
};

LC 539. Minimum Time Difference的更多相关文章

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

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

  2. 539. Minimum Time Difference

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

  3. 539 Minimum Time Difference 最小时间差

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

  4. Python解Leetcode: 539. Minimum Time Difference

    题目描述:给定一个由时间字符组成的列表,找出任意两个时间之间最小的差值. 思路: 把给定的链表排序,并且在排序的同时把60进制的时间转化成十进制整数: 遍历排序的数组,求出两个相邻值之间的差值: 求出 ...

  5. 530.Minimum Absolute Difference in BST 二叉搜索树中的最小差的绝对值

    [抄题]: Given a binary search tree with non-negative values, find the minimum absolute difference betw ...

  6. 51. leetcode 530. Minimum Absolute Difference in BST

    530. Minimum Absolute Difference in BST Given a binary search tree with non-negative values, find th ...

  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. 530. Minimum Absolute Difference in BST

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

  9. [LeetCode] Minimum Time Difference 最短时间差

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

随机推荐

  1. 实现单点登录功能的思路以及kafka同步数据

    单点登录以及用户数据同步思路与方案 当公司业务分布于多个子系统时, 同一用户在A系统注册,即可在其他所有关联系统使用, 并支持登录A系统后,自动在其他系统登录,退出同理. 在A平台修改通用的用户数据( ...

  2. nginx反向代理局域网访问外网

    .配置内网hosts vim /etc/hosts 添加 host1(能连外网的服务器ip)   central.maven.org 2.在host1 服务器上nginx配置 server {    ...

  3. js基本事件

    1:单击事件 --onclick function test01(){ alert("js的单击事件"); } 2:双击事件--ondblclick function test02 ...

  4. CAN学习方法(知乎)

    作者:心机之花链接:https://www.zhihu.com/question/26776219/answer/244433861来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...

  5. 自动匹配输入的内容(AutoCompleteTextView及MultiAutoCompleteTextView)

    自动匹配输入的内容 AutoCompleteTextView 1.功能动态匹配输入的内容,如百度搜索引擎当输入文本时,可以根据内容显示匹配的热门信息 2.属性:android:completionTh ...

  6. 5.NIO_ Selector选择器

    1.阻塞与非阻塞 传统的 IO 流都是阻塞式的.也就是说,当一个线程调用 read() 或 write() 时,该线程被阻塞,直到有一些数据被读取或写入, 该线程在此期间不能执行其他任务因此,在完成网 ...

  7. Linux 之Ubuntu在VM中安装(桌面版)

    1.安装系统 https://jingyan.baidu.com/article/14bd256e0ca52ebb6d26129c.html 2.安装VM Tools https://jingyan. ...

  8. Linux之checkconfig 服务自启动

    chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. 使用语法: chkconfig [--a ...

  9. tensorflow2.0 在pycharm下提示问题

    tensorflow2.0 使用keras一般通过tensorflow.keras来使用,但是pycharm没有提示,原因是因为实际的keras路径放在tensorflow/python/keras, ...

  10. nginx之"/"结尾

    一.配置127.0.0.1:8081的tomcat下的文件: 1.ROOT/a.html : this is ROOT page 2.ROOT/testa.html : nihao 3.ROOT/in ...