当前位置:网站首页>剑指 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]);
}
边栏推荐
- Three postures of anti CSRF blasting
- 【微信小程序】认识小程序项目的基本组成结构
- I wonder if I can open an account today? In addition, is it safe to open an account online now?
- Inspiration collection · evaluation of creative writing software: flomo, obsidian memo, napkin, flowus
- Solr basic operations 9
- [advanced C language] string and memory function (I)
- Ingenious application of golang generics to prevent null pointer errors of variables and structural fields
- Leetcode (633) -- sum of squares
- Basic tutorial for installing monggodb in win10
- Solr基础操作11
猜你喜欢

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

koa2学习和使用

Basic tutorial for installing monggodb in win10

Binary search tree 230 The element with the smallest K in the binary search tree 1038 From binary search tree to larger sum tree

ThinkPad VMware installation virtual machine: this host supports Intel VT-x, but Intel VT-x is disabled (problem resolution)

About mongodb error: connecting to: mongodb://127.0.0.1:27017/?compressors=disabled &gssapiServiceName=mongodb

Vulnhub靶机-MoriartyCorp

6.28日刷题题解

Cloud native enthusiast weekly: cool collection of grafana monitoring panels

数莓派 4怎么样?可能的玩法有哪些?
随机推荐
6.29日刷题题解
手机开户后多久才能通过?另外,手机开户安全么?
8软件工程环境
Bee common configuration
Exploration and Practice on the future direction of byte cloud database
Binary search tree 230 The element with the smallest K in the binary search tree 1038 From binary search tree to larger sum tree
Simple understanding of B tree and b+ tree
Andorid source build/envsetup.sh 该知道的细节
Shell positional parameter variables and predefined variables
云原生爱好者周刊:炫酷的 Grafana 监控面板集合
History of deep learning
QT learning 04 Hello QT
西门子低代码平台通过Database Connector 连接Mysql 实现增删改查
Vulnhub靶机-MoriartyCorp
Which securities company is good for opening a mobile account? In addition, is it safe to open a mobile account?
Three postures of anti CSRF blasting
About mongodb error: connecting to: mongodb://127.0.0.1:27017/?compressors=disabled &gssapiServiceName=mongodb
多数元素II[求众数类之摩尔投票法]
Zhongkang holdings opens the offering: it plans to raise HK $395million net, and it is expected to be listed on July 12
Solr basic operations 9