题目地址
https://leetcode.com/problems/minimum-time-difference/
题目描述
539. Minimum Time Difference
Given a list of 24-hour clock time points in "HH:MM" format, return the minimum minutes difference between any two time-points in the list.
Example 1:
Input: timePoints = ["23:59","00:00"]
Output: 1
Example 2:
Input: timePoints = ["00:00","23:59","00:00"]
Output: 0
思路
- 将string转换成分钟int的list.
- 排序后两两求差值的最小值.
关键点
- 注意, 0点和23:59分这种边界条件. 最后一个和第一个差值的处理.
代码
- 语言支持:Java
class Solution {
public int findMinDifference(List<String> timePoints) {
List<Integer> times = new ArrayList<>();
for (String str: timePoints) {
String[] strs = str.split(":");
int h1 = Integer.parseInt(strs[0]);
int m1 = Integer.parseInt(strs[1]);
times.add(h1 * 60 + m1);
}
Collections.sort(times);
int min = Integer.MAX_VALUE;
for (int i = 1; i < times.size(); i++) {
min = Math.min(min, times.get(i) - times.get(i - 1));
}
min = Math.min(min, 24 * 60 - times.get(times.size() - 1) + times.get(0));
return min;
}
}
网友评论