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 ...
随机推荐
- bisect:维护一个有序的列表
介绍 bisect模块实现了一个算法来向列表中插入元素,同时仍然保证列表有序 有序插入 import bisect ''' 可以使用bisect.insort向一个列表中插入元素 ''' values ...
- task_struct源码解读
task_struct英文源码原文 以下是中文以及解释:(未完待续,慢慢敲) 1. /* Used in tsk->state: */ #define TASK_RUNNING 0x0000// ...
- python函数:匿名函数、函数递归与二分法、面向过程编程
今天主要讲三大部分内容: 一.匿名函数二.函数递归与二分法三.面向过程编程 一.匿名函数: """ 1. 什么时匿名函数 def定义的是有名函数:特点是可以通过名字重复调 ...
- iptables 转发
1.通过代理访问121.8.210.236的转向访问192.168.191.236 sudo iptables -t nat -A OUTPUT -d 121.8.210.236 -j DNAT -- ...
- PLSQL功能一览(1/2)
用了Oracle几年了,除了PLSQL几乎就没用过别的工具.临时起义想看看PLSQL有哪些功能是我平时没注意的,别是一直有好办法,我却用着笨办法. 本文针对PLSQL12.0.7 1.登录以后使用My ...
- java中activiti框架中的排他网关使用方法,多条件判断
当排他网关的判断条件中出现多个条件时,需要注意,设置判断条件时,可能遇到,流向相同的任务,而判断条件的变量个数不同 那么,必须在后面的运行任务时,将所有的涉及到的变量都设置进任务中,只不过,如果这个任 ...
- BZOJ 1002 生成树计数&高精度
给你定义一种特殊的图 这种图总共有n个节点 假设编号为0~n-1 首先1~n-1排成环形 每个点与相邻的两个点有边 其次这n-1个节点每个和0节点有一条边 每次询问你一个n 要回到当前n节点的特殊图有 ...
- Summer training #6
A:水.看0多还是1多就行 B:模拟二进制运算 ,,卡了好久 不应该 #include <bits/stdc++.h> #include <cstring> #include ...
- Flutter入门(三)-底部导航+路由
* StatefulWidget 如果想改变页面中的数据就要用到StatefulWidget,之前自定义组件继承的StatelessWidget是不能动态修改页面数据的 //自定义有状态组件 clas ...
- Tomcat与Jetty比较
Jetty 基本架构 Jetty目前的是一个比较被看好的 Servlet 引擎,它的架构比较简单,也是一个可扩展性和非常灵活的应用服务器. 它有一个基本数据模型,这个数据模型就是 Handler(处理 ...