LC 539. Minimum Time Difference
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:
- The number of time points in the given list is at least 2 and won't exceed 20000.
- 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的更多相关文章
- 【LeetCode】539. Minimum Time Difference 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/minimum-t ...
- 539. Minimum Time Difference
Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minut ...
- 539 Minimum Time Difference 最小时间差
给定一个 24 小时制(小时:分钟)的时间列表,找出列表中任意两个时间的最小时间差并已分钟数表示.示例 1:输入: ["23:59","00:00"]输出: 1 ...
- Python解Leetcode: 539. Minimum Time Difference
题目描述:给定一个由时间字符组成的列表,找出任意两个时间之间最小的差值. 思路: 把给定的链表排序,并且在排序的同时把60进制的时间转化成十进制整数: 遍历排序的数组,求出两个相邻值之间的差值: 求出 ...
- 530.Minimum Absolute Difference in BST 二叉搜索树中的最小差的绝对值
[抄题]: Given a binary search tree with non-negative values, find the minimum absolute difference betw ...
- 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 ...
- LeetCode 530. Minimum Absolute Difference in BST (二叉搜索树中最小绝对差)
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- 530. Minimum Absolute Difference in BST
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- [LeetCode] Minimum Time Difference 最短时间差
Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minut ...
随机推荐
- 视频大文件分片上传(使用webuploader插件)
背景 公司做网盘系统,一直在调用图片服务器的接口上传图片,以前写的,以为简单改一改就可以用 最初要求 php 上传多种视频格式,支持大文件,并可以封面截图,时长统计 问题 1.上传到阿里云服务器,13 ...
- 【异常】553 Mail from must equal authorized user
1 详细异常打印 2019-08-12 14:54:42,178 ERROR org.apache.camel.processor.DefaultErrorHandler: Failed delive ...
- centos6.4升级openssh7.4p1
Centos6.4版本yum升级openssh版本最高到5.3,想要升级到更高的版本需要重新编译 一.查看当前openssh版本: [root@localhost ~]# ssh -VOpenSSH_ ...
- python常用模块:sys、os、path、setting、random、shutil
今日内容讲了3个常用模块 一.sys模块二.os模块三.os下path模块四.random模块五.shutil模块 一.sys模块 import sys #环境变量 print(sys.path) # ...
- java中有个很强大的工具jconsole.exe
这个工具可以监控java程序的线程,cpu和内存使用情况.
- 查看电脑物理地址(MAC)方法
首先打开电脑,按ctrl+R键,将会出现以下界面 然后直接点击确认即可,会出现管理员界面,如下 我们现在有两种查看MAC地址的方法: 方法一:.直接输入ipconfig/all(或者输入ipconfi ...
- unsigned char数组赋值
memset(send_buf, 0, SEND_BUFF_LEN); const char * pStr = "this is test txt"; strcpy((char*) ...
- MyBatis Generator 移除字段前缀
在table标签内添加 <columnRenamingRule searchString="wrc_" replaceString=""/> < ...
- 201871010101-陈来弟《面向对象程序设计(java)》第十七周学习总结
实验十七 线程同步控制 实验时间 2018-12-10 第一部分:理论知识 1.多线程并发执行中的问题 ◆多个线程相对执行的顺序是不确定的. ◆线程执行顺序的不确定性会产生执行结果的不确定性. ◆在 ...
- Python模拟百度自动输入搜索功能
# 访问百度,模拟自动输入搜索 # 代码中引入selenium版本为:3.4.3 # 通过Chrom浏览器访问发起请求 # Chrom版本:59 ,chromdriver:2.3 # 需要对应版本的C ...