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.
题目含义:给定一定24格式的Hour:Minutes字符,找到任意两个时间点的最小时间差
1 public int findMinDifference(List<String> timePoints) {
2 boolean[] minutes = new boolean[24 * 60];
3 for (String time : timePoints) {
4 String[] values = time.split(":");
5 int minute = Integer.valueOf(values[0]) * 60 + Integer.valueOf(values[1]);
6 if (minutes[minute]) return 0;
7 minutes[minute] = true;
8 }
9 int min = Integer.MAX_VALUE, left = Integer.MAX_VALUE, right = Integer.MIN_VALUE;
10 int previous = 0;//上一个时间值
11 for (int i = 0; i < minutes.length; i++) {
12 if (!minutes[i]) continue;
13 if (left != Integer.MAX_VALUE) {
14 min = Math.min(min, i - previous);//min记录了最小的时间差
15 }
16 left = Math.min(left, i);//距离零点最近的点
17 right = Math.max(right, i);//距离零点最远的点
18 previous = i;
19 }
20 return Math.min(min, 24 * 60 - right + left);
21 }
539. Minimum Time Difference的更多相关文章
- LC 539. Minimum Time Difference
Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minut ...
- 【LeetCode】539. Minimum Time Difference 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/minimum-t ...
- 539 Minimum Time Difference 最小时间差
给定一个 24 小时制(小时:分钟)的时间列表,找出列表中任意两个时间的最小时间差并已分钟数表示.示例 1:输入: ["23:59","00:00"]输出: 1 ...
- Python解Leetcode: 539. Minimum Time Difference
题目描述:给定一个由时间字符组成的列表,找出任意两个时间之间最小的差值. 思路: 把给定的链表排序,并且在排序的同时把60进制的时间转化成十进制整数: 遍历排序的数组,求出两个相邻值之间的差值: 求出 ...
- 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 ...
- [LeetCode] Minimum Absolute Difference in BST 二叉搜索树的最小绝对差
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
随机推荐
- Xftp设置指定记事本(notepad++)打开文件
右键
- 【LeetCode】368. Largest Divisible Subset 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/largest-d ...
- Shell实现文件内容批量替换的方法
在Linux系统中,文件内容的批量替换同Windows平台相比要麻烦一点.不过这里可以通过Shell命令或脚本的方式实现批量替换的功能. 笔者使用过两个命令:perl和sed ,接下来会对其做出说明. ...
- 使用.NET 6开发TodoList应用(15)——实现查询搜索
系列导航及源代码 使用.NET 6开发TodoList应用文章索引 需求 本文我们继续来看查询过程中的另外一个需求:搜索.搜索的含义是目标字段的全部或者部分值匹配请求中的搜索条件,对应到数据库层面是C ...
- http协议的状态码400,401,403,404,500,502,503,301,302等常见网页错误代码
1xx(临时响应)表示临时响应并需要请求者继续执行操作的状态码. 100(继续) 请求者应当继续提出请求.服务器返回此代码表示已收到请求的第一部分,正在等待其余部分. 101(切换协议) 请求者已要求 ...
- Error: Cannot find module '@dcloudio/uni-cli-i18n' 解决方案
这个错误是因为node_modules缺少了 '@dcloudio/uni-cli-i18n' 以下是错误信息 解决方案: yarn add -D @dcloudio/uni-cli-i18n ...
- RSA非对称加密算法实现:Golang
RSA是1977年由罗纳德·李维斯特(Ron Rivest).阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)一起提出的.当时他们三人都在麻省理工学院工作.RSA ...
- Swoole 协程性能测试
// 开启协程化,文件操作,sleep,Mysqli,PDO,streams等都变成异步IO Co::set(['hook_flags'=> SWOOLE_HOOK_ALL]); $s = mi ...
- linux(CentOS7) 之 jdk1.8 下载及安装
下载 一.百度搜索oracle,进入官网(或直接进入官网https://www.oracle.com) 二.选择 products 下的 java 三.选择Download Java 四.往下翻找到J ...
- 初识python: 集合
集合是一个无序的,不重复的数据组合.主要作用如下:1.去重,把一个列表变成集合,会自动去重:2.关系测试,测试两组数据之前的交集.差集.并集等关系 #!/user/bin env python # a ...