当前位置:网站首页>剑指 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]);
}
边栏推荐
- There is no web-based development for the reward platform. Which is suitable for native development or mixed development?
- How to write controller layer code gracefully?
- JVM之栈空间
- Machine learning: the concept and application of VC dimension
- 基于zfoo开发项目的一些规范
- Solr基础操作9
- Shell operator
- Basic tutorial for installing monggodb in win10
- Zhongang Mining: Fluorite helps the construction and development of lithium battery in fluorine industry
- Solr basic operations 7
猜你喜欢

New titanium cloud service won the "2022 love analysis · panoramic report of it operation and maintenance manufacturers" cloud management platform CMP representative manufacturer

How to view the CPU cores and threads in win11? Win11 view the tutorial of how many cores and threads the CPU is

Exploration and Practice on the future direction of byte cloud database

Sword finger offer 15 Number of 1 in binary

数莓派 4怎么样?可能的玩法有哪些?

JVM之栈空间

JS绘制极坐标颜色渐变

8软件工程环境

由GlideException: Failed DecodePath{DirectByteBuffer->GifDrawable->Drawable}引起的刨根问底

Activity invitation | the Apache Doris community essay and speech solicitation activity has begun!
随机推荐
Some specifications based on zfoo development project
Solr基础操作8
Analysis of common vlog parameters
QPainter的使用入门:绘制象棋界面
Copy linked list with random pointer [space for time --hash record]
手机开户后多久才能通过?另外,手机开户安全么?
MySQL functions and constraints
克隆无向图[bfs访问每条边而不止节点]
Sword finger offer 15 Number of 1 in binary
Solr basic operations 13
Rotating colored clover
Solr基础操作13
Will the flush SQL CDC parallelism affect the order? Generally, only 1 can be set for data synchronization.
FPGA Development (1) -- serial port communication
Solr基础操作7
Solr基础操作10
數莓派 4怎麼樣?可能的玩法有哪些?
6.29 problem solving
如何实现搜索引擎中的拼写纠错功能——思路
Table responsive layout tips for super nice