LeetCode 539. 最小时间差
539. 最小时间差
Solution
思路:
将所有时间字符串排序后 最小时间只可能是 前后相邻时间 和 首位时间。1440可以剪枝处理,因为24*60。
class Solution {
public int findMinDifference(List<String> timePoints) {
if (timePoints.size() > 1440) {
return 0;
}
Collections.sort(timePoints);
int ans = Integer.MAX_VALUE;
int minTime = getMinute(timePoints.get(0));
int preTime = minTime;
for (int i = 1; i < timePoints.size(); i++) {
int curTime = getMinute(timePoints.get(i));
ans = Math.min(ans, curTime - preTime);
preTime = curTime;
}
ans = Math.min(ans, minTime + 1440 - preTime);
return ans;
}
public int getMinute(String time) {
return ((time.charAt(0) - '0') * 10 + time.charAt(1) - '0') * 60 + (time.charAt(3) - '0') * 10 + time.charAt(4) - '0';
}
}
埋骨何须桑梓地,人生无处不青山