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. Vue页面缓存和不缓存的方法

    第一步 在app中设置需要缓存的div //缓存的页面 <keep-alive> <router-view v-if="$route.meta.keepAlive" ...

  2. Delphi 图像组件(Image)

    樊伟胜

  3. Atmel芯片使用

    ATMEL系列芯片 9X35 9G35可pin-to-pin替代9G10,具体需核对.此外即使pin-to-pin替代,外部应用也不一样. A5D2处理器,可支持linux/andriod. M7(M ...

  4. zabbix 自定义Key (六)

    1.在zabbix_agent端zabbix_agentd.conf配置文件中增加自定义Key(/usr/local/zabbix_agent/etc/zabbix_agentd.conf) ### ...

  5. java8学习之Function与BiFunction函数式接口详解

    Function接口: 上次中已经使用了Function的apply()方法,但是在这个接口中还存在三个具体实现的方法,如下: 下面来仔细的将剩下的方法学习一下: compose(): 首先来读一下该 ...

  6. P2634 树上路径长度为3的倍数的点对数 点分治

    在计算答案的时候维护一个数组num num[i]为当前所有点距离根距离%3的数量 则当前块的答案为num[0]*num[0]+2*num[1]*num[2] #include<bits/stdc ...

  7. OI视角浅谈布隆过滤器

    简要谈及布隆过滤器 Preface 不负责的出题人扔了一道5e5,2M卡内存的题,标算布隆过滤器,然而std自己用std::set 70M碾过去了. 没学OI时候草草看过这个,不过忘得差不多了. 今天 ...

  8. MySQL 关于触发器的操作

    -- 监视某种情况,并触发某种操作 --触发器创建语法四要素 1.监视地点 2.监视事件 3.触发时间 4.触发事件 -- 触发器只能创建在永久表上,不能对临时表创建触发器 create trigge ...

  9. (已解决)FVDI 2018“连接到服务器.....失败”“打不开设备”

    FVDI 2018 错误和解决方案来自网站:eobdtool.co.uk FVDI 2018发现以下错误: “连接到服务器.....失败” “设备未打开” 解决方案: 请下载FDVI 2018 V3. ...

  10. jmeter 5.0,http请求登录返回的cookie在头部处理方法

    http登录之后返回的cookie在响应的头部,再次请求虽然加了cookie管理器,但是下一个请求调用响应是登陆失效,这里讲一下我的解决方法 1:在登录之后添加正则表达式,提取cookie 2:提取之 ...