leetcode 539. Minimum Time Difference
来源:互联网 发布:域名mx记录 编辑:程序博客网 时间:2024/06/05 16:53
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.
我是想反正时间固定是时从0~23,分从0~59,那么我可以用一个数组来记录下。然后找这个数组中相邻两个之间差最小的。但是这没有考虑到数组中时间有重复的情况,所以在一开始存入数组中的时候,一旦发现重复的时间点,则返回0.
package leetcode;import java.util.ArrayList;import java.util.List;public class Minimum_Time_Difference_539 {public int findMinDifference(List<String> timePoints) {int[][] time=new int[24][60];int min=Integer.MAX_VALUE;for(String timeString:timePoints){String[] split=timeString.split(":");int hour=Integer.valueOf(split[0]);int minute=Integer.valueOf(split[1]);if(time[hour][minute]==1){return 0;}else{time[hour][minute]=1;}}int i,j=0;int[] before=new int[2],current=new int[2];boolean isFindBefore=false;int[] linchenfirst=new int[2];//保留凌晨第一个时间,用来与前一天最后一个时间相减for(i=0;i<24;i++){for(j=0;j<60;j++){if(time[i][j]==1){if(isFindBefore==false){before[0]=i;before[1]=j;linchenfirst[0]=i;linchenfirst[1]=j;isFindBefore=true;}else{current[0]=i;current[1]=j;int difference=difference(before, current);min=min<=difference?min:difference;before[0]=i;before[1]=j;}}}}int difference=difference(current, linchenfirst);min=min<=difference?min:difference;return min;}public int difference(int[] before,int[] current){if(before[0]==current[0]){return Math.abs(current[1]-before[1]);}else if(before[0]<current[0]){int minute=(current[0]-before[0])*60;if(before[1]<current[1]){minute+=current[1]-before[1];}else{minute-=before[1]-current[1];}return minute;}else{//一般current都会比before大的,这种情况应该是过了凌晨了current[0]+=24;return difference(before, current);}}public static void main(String[] args) {// TODO Auto-generated method stubMinimum_Time_Difference_539 m=new Minimum_Time_Difference_539();List<String> list=new ArrayList<String>();list.add("00:00");list.add("23:59");list.add("00:00");System.out.println(m.findMinDifference(list));}}大神思路跟我一样,不过解法更妙更简洁。直接把时间算成hour*60+minute,就只有 24 * 60 = 1440 可能的时间点。那么可以用bucket sort,如果存在那个时间点,就在那个时间点的桶里放true.
public class Solution { public int findMinDifference(List<String> timePoints) { boolean[] mark = new boolean[24 * 60]; for (String time : timePoints) { String[] t = time.split(":"); int h = Integer.parseInt(t[0]); int m = Integer.parseInt(t[1]); if (mark[h * 60 + m]) return 0; mark[h * 60 + m] = true; } int prev = 0, min = Integer.MAX_VALUE; int first = Integer.MAX_VALUE, last = Integer.MIN_VALUE; for (int i = 0; i < 24 * 60; i++) { if (mark[i]) { if (first != Integer.MAX_VALUE) { min = Math.min(min, i - prev); } first = Math.min(first, i); last = Math.max(last, i); prev = i; } } min = Math.min(min, (24 * 60 + first - last)); return min; }}first和last分别是记录数组中时间最小的时间点和时间最大的时间点,用来计算凌晨跨度的时间差。
阅读全文
0 0
- leetcode 539. Minimum Time Difference
- Leetcode-539. Minimum Time Difference
- [leetcode] 539. Minimum Time Difference
- [LeetCode]539. Minimum Time Difference
- LeetCode 539. Minimum Time Difference
- leetcode 539. Minimum Time Difference
- LeetCode 539. Minimum Time Difference
- leetcode 539. Minimum Time Difference
- LeetCode:539. Minimum Time Difference
- leetcode 539. Minimum Time Difference
- LeetCode笔记:539. Minimum Time Difference
- [Leetcode] 539. Minimum Time Difference 解题报告
- 539. Minimum Time Difference
- 539. Minimum Time Difference
- 539. Minimum Time Difference
- 539. Minimum Time Difference
- 539.Minimum Time Difference
- 【LeetCode 539】 Minimum Time Difference
- 采集百度搜索的方法
- CADisplayLink
- id nil Nil SET IMP 等概念
- java中 static变量和方法到底是存在内存什么区域?
- json传递含有html的json
- leetcode 539. Minimum Time Difference
- Objective-c selector学习
- 算法导论动态规划实现
- 游戏编程入门(18):使用闪屏增添游戏的活力
- 深入浅出 iOS 之生命周期
- loadView和viewDidLoad及initWithNibName/awakeFromNib/initWithCoder
- DynamicsPerf 2.0 Deployment Guide
- initWithNibName和viewDidLoad执行顺序
- JSON格式-给后台传递数据时需要注意的地方