码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode-539-Minimum Time Difference

时间:2017-03-22 00:36:18      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:tween   etc   https   list   ++   range   problems   scribe   data   

leetcode-539-Minimum Time Difference

539. Minimum Time Difference

  • Total Accepted: 3164
  • Total Submissions: 7087
  • Difficulty: Medium
  • Contributors: fallcreek

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:

  1. The number of time points in the given list is at least 2 and won‘t exceed 20000.
  2. The input time is legal and ranges from 00:00 to 23:59.

 

 

Subscribe to see which companies asked this question.

 

题解:

简单的时钟问题; (1), 先对时间进行排序, (2),计算头尾即可

 

 

class Solution {
public:
    int GetNum(string tp){
        return (60*((tp[0]-‘0‘)*10 + (tp[1]-‘0‘)) + (tp[3]-‘0‘)*10 + tp[4]-‘0‘); 
    } 
    int findMinDifference(vector<string>& timePoints) {
        int len = timePoints.size(); 
        if(len <= 1){
            return 0; 
        }
        sort(timePoints.begin(), timePoints.end()); 
        int tmp, ans = 24*60-GetNum(timePoints[len-1]) + GetNum(timePoints[0]); 
        for(int i=0; i<len-1; ++i){
            tmp = GetNum(timePoints[i+1]) - GetNum(timePoints[i]); 
            if(tmp < ans){
                ans = tmp; 
            }
        }
        return ans; 
    }
};

  

 

leetcode-539-Minimum Time Difference

标签:tween   etc   https   list   ++   range   problems   scribe   data   

原文地址:http://www.cnblogs.com/zhang-yd/p/6597224.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!