当前位置:网站首页>剑指 Offer II 035. 最小时间差
剑指 Offer II 035. 最小时间差
2022-06-30 00:04:00 【小白码上飞】
概要
用一个长度为60*24=1440的数组,标记出现的时间点。之后计算两两之间的距离。
题目
给定一个 24 小时制(小时:分钟 “HH:MM”)的时间列表,找出列表中任意两个时间的最小时间差并以分钟数表示。

链接:https://leetcode.cn/problems/569nqc/
思路
时间列表是24小时制,且精确到分。24小时,有1440分钟。
我们可以用一个长度为1440的数组,作为一个哈希表。将每一分钟落到每一个格子里。之后遍历每一个格子,记录有值的格子之间的最短距离。当然,别忘了这一天的最早时间点和最晚时间点之间的时间差,也要参与到计算中。
解法:长度1440的数组记录时间
代码
public int findMinDifference(List<String> timePoints) {
int[] list = new int[1440];
for (String timePoint : timePoints) {
// 有相同的时间点,直接返回0
int hash = hash(timePoint);
if (list[hash] == 1) {
return 0;
}
list[hash]++;
}
int min = Integer.MAX_VALUE;
int start = 0;
int first = 0;
// 初始化起始节点
for (int i = 0; i < list.length; i++) {
if (list[i] != 0) {
start = i;
first = i;
break;
}
}
for (int i = start + 1; i < list.length; i++) {
if (list[i] > 0) {
min = Math.min(min, i - start);
start = i;
}
}
return Math.min(min, list.length - start + first);
}
private int hash(String timePoint) {
String[] split = timePoint.split(":");
return Integer.parseInt(split[0]) * 60 + Integer.parseInt(split[1]);
}
边栏推荐
- JS绘制极坐标颜色渐变
- Stack space of JVM
- modelsim的TCL脚本的define incdir命令解析
- Will the flush SQL CDC parallelism affect the order? Generally, only 1 can be set for data synchronization.
- Binary search tree 230 The element with the smallest K in the binary search tree 1038 From binary search tree to larger sum tree
- Cloud native enthusiast weekly: cool collection of grafana monitoring panels
- Sword finger offer 14- I. cut rope
- Solr基础操作15
- Basic tutorial for installing monggodb in win10
- Bee常用配置
猜你喜欢

Siemens low code platform connects MySQL through database connector to realize addition, deletion, modification and query

Sword finger offer 14- ii Cutting rope II

8软件工程环境

QT learning 07 coordinate system in QT

这次的PMP考试(6月25日),有人欢喜有人忧,原因就在这...

6.29 problem solving

FPGA Development (1) -- serial port communication

Sword finger offer 15 Number of 1 in binary

After 8 years of polishing, "dream factory of game design" released an epic update!

西门子低代码平台通过Database Connector 连接Mysql 实现增删改查
随机推荐
Solr基础操作14
Label Troubleshooting: unable to open the marked image
數莓派 4怎麼樣?可能的玩法有哪些?
手机开户后多久才能通过?另外,手机开户安全么?
Viewing splitchunks code segmentation from MPX resource construction optimization
Golang泛型的巧妙应用,防止变量空指针错误,防止结构体字段空指针错误
500 error occurred after importing skins folder into solo blog skin
Koa2 learning and using
Machine learning: the concept and application of VC dimension
视频ToneMapping(HDR转SDR)中的颜色空间转换问题(BT2020转BT709,YCbCr、YUV和RGB)
Cartoon security HIDS, EDR, NDR, XDR
gyctf_ 2020_ document
Zhongang Mining: Fluorite helps the construction and development of lithium battery in fluorine industry
Construction of module 5 of actual combat Battalion
Sword finger offer 14- I. cut rope
如何实现搜索引擎中的拼写纠错功能——思路
Fine grained identification, classification, retrieval data set collation
Andorid source build/envsetup. SH details to know
Solr basic operations 9
Solr基础操作16