public class {
public int findMinDifference(List<String> timePoints) {
Collections.sort(timePoints);
int res = Integer.MAX_VALUE;
for( int i = 1; i < timePoints.size(); i++) {
int diff = getDiff(timePoints.get(i-1), timePoints.get(i));
res = Math.min(diff, res);
}
int val1 = getDiff(timePoints.get(timePoints.size()-1), "24:00");
int val2 = getDiff("00:00", timePoints.get(0));
res = Math.min((val1+val2), res);
return res;
}
private int getDiff(String str1, String str2) {
int h1 = Integer.parseInt(str1.substring(0,2));
int h2 = Integer.parseInt(str2.substring(0,2));
int m1 = Integer.parseInt(str1.substring(3,5));
int m2 = Integer.parseInt(str2.substring(3,5));
int diff = 60 * (h2-h1) - m1 + m2;
return diff;
}
}
近期评论