ID | Title | Difficulty | |
---|---|---|---|
Loading... |
539. Minimum Time Difference
Medium
LeetCode
Array, Math, String, Sorting
Problem
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
Constraints:
- 2 <= timePoints.length <= 2 * 10^4
- timePoints[i] is in the format “HH:MM”.
Code
class Solution {
public int findMinDifference(List<String> timePoints) {
int total = 24 * 60;
boolean[] dict = new boolean[total];
for(String time : timePoints){
int h = Integer.valueOf(time.split(":")[0]);
int m = Integer.valueOf(time.split(":")[1]);
// 重复的时间
if(dict[h * 60 + m]) return 0;
dict[h * 60 + m] = true;
}
int minDiff = total + 1;
int first = -1;
int prev = -1;
for(int i = 0; i < total; i++){
if(dict[i] == true){
if(first == -1) {
first = i;
} else {
minDiff = Math.min(minDiff, i - prev);
}
prev = i;
}
}
minDiff = Math.min(minDiff, (total - (prev - first)));
return minDiff;
}
}
按 <- 键看上一题!
538. Convert BST to Greater Tree
按 -> 键看下一题!
540. Single Element in a Sorted Array