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. SpringBoot-自动配置

    一.自动配置的核心 一般springboot程序,我们都会在主启动类上加上@SpringBootApplication注解,@SpringBootApplication是一个复合注解,上面标注了@Co ...

  2. SpringBoot static修饰的字段/方法如何获取application.yml配置

    SpringBoot的application.yml一种特殊的应用场景,一般我们获取application.yml的配置文件只要@Value就可以获取到值了,但是如果是static修饰的字段肯定就不能 ...

  3. MacBook Pro实现共享屏幕(多台mac之间)

    1 开启允许所有用户控制屏幕     设置— 共享           2 在另一台MacBook Pro里打开finder窗口,并设置好network显示边栏  3  另一台机器发送请求控制   找 ...

  4. busybox date 时间的加减

    1.下载安装busybox: # wget http://busybox.net/downloads/busybox-1.29.3.tar.bz2 # tar -jxvf busybox-.tar.b ...

  5. Mysql(三)-1:存储引擎

    一 什么是存储引擎 mysql中建立的库===>文件夹 库中建立的表===>文件 现实生活中我们用来存储数据的文件有不同的类型,每种文件类型对应各自不同的处理机制:比如处理文本用txt类型 ...

  6. main特别之处

    //package new_Object; public class Main{ public static void main(String[] args) { System.out.println ...

  7. deep_learning_Function_tensorboard的使用

    数据可视化(网页能打开,但是没有数据):https://jingyan.baidu.com/article/e9fb46e1c55ac93520f7666b.html

  8. Ubuntu 16.04 装机后如何永久更改ulimit和修改MySQL的存储路径datadir

    Ubuntu 16.04 装机后的配置要点: 1. 网络的配置 2. 更改源列表 3. 永久更改ulimit ulimit限制着程序打开文件的数目,默认情况下为1024,作为服务器使用时,这个数字往往 ...

  9. zencart清空产品商品实用命令

    TRUNCATE TABLE categories; TRUNCATE TABLE categories_description;TRUNCATE TABLE meta_tags_categories ...

  10. Dijkstra 优先队列优化

    #include <iostream> #include <queue> #include <vector> using namespace std; ; stru ...