当前位置:网站首页>Sword finger offer II 035 Minimum time difference
Sword finger offer II 035 Minimum time difference
2022-06-30 00:18:00 【Small white yards fly up】
Summary
Use a length of 60*24=1440 Array of , The point in time when the marker appears . Then calculate the distance between the two .
subject
Given a 24 hourly ( Hours : minute “HH:MM”) Time list for , Find the minimum time difference between any two times in the list and express it in minutes .

link :https://leetcode.cn/problems/569nqc/
Ideas
Time list is 24 hourly , And accurate to points .24 Hours , Yes 1440 minute .
We can use a length of 1440 Array of , As a hash table . Drop every minute into every grid . Then traverse each lattice , Record the shortest distance between grids with values . Of course , Don't forget the difference between the earliest and the latest time of the day , Also participate in the calculation .
solution : length 1440 Array of records time
Code
public int findMinDifference(List<String> timePoints) {
int[] list = new int[1440];
for (String timePoint : timePoints) {
// Have the same time point , Go straight back to 0
int hash = hash(timePoint);
if (list[hash] == 1) {
return 0;
}
list[hash]++;
}
int min = Integer.MAX_VALUE;
int start = 0;
int first = 0;
// Initialize start node
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]);
}
边栏推荐
- QT learning 04 Hello QT
- 数据中台的五个关键要素
- Cloner un Graphe non recté [bfs accède à chaque bord et pas seulement aux noeuds]
- Introduction to reptiles: data capture of Betta barrage, with 11 introductory notes attached
- 复制带随机指针的链表[空间换时间--hash记录]
- 手机开户后多久才能通过?另外,手机开户安全么?
- 01背包问题
- [QNX Hypervisor 2.2用户手册]6.2.2 Guest与Host之间通信
- Buffer flow exercise
- Basic operations such as MySQL startup under Windows platform
猜你喜欢

Siemens low code version 9.14: meet different needs

QT learning 07 coordinate system in QT

Zhongang Mining: Fluorite helps the construction and development of lithium battery in fluorine industry

云呐|如何利用系统管理固定资产?如何进行固定资产管理?

JVM之栈空间

Color space conversion in video tonemapping (HDR to SDR) (bt2020 to bt709, YCbCr, YUV and RGB)
![克隆无向图[bfs访问每条边而不止节点]](/img/34/2a1b737b6095293f868ec6aec0ceeb.png)
克隆无向图[bfs访问每条边而不止节点]

剑指 Offer II 037. 小行星碰撞

Quick Pow: 如何快速求幂

How to write controller layer code gracefully?
随机推荐
Connection query of SQL Server database
Getting started with qpainter: drawing the chess interface
Solr basic operation 6
Solr basic operations 13
[advanced C language] string and memory function (II)
[dynamic programming] - linear DP
克隆无向图[bfs访问每条边而不止节点]
公司固定资产该哪个部门管理,一般公司固定资产怎么管理
云呐|固定资产系统管理,nc系统管理固定资产在哪里
Stream collectors usage
Solr基础操作6
QT learning 04 Hello QT
基于zfoo开发项目的一些规范
Embedded development: Hardware in the loop testing
vsftp 与 TFTP 与 samba 与 nfs 复习
旋转彩色三叶草
QT learning 06 widgets and window types
Rotating colored clover
云呐|固定资产系统管理的优势,固定资产管理系统有何特点
如何实现搜索引擎中的拼写纠错功能——思路