539 Minimum Time Difference 最小时间差

给定一个 24 小时制(小时:分钟)的时间列表,找出列表中任意两个时间的最小时间差并已分钟数表示。
示例 1:
输入: ["23:59","00:00"]
输出: 1
备注:
    1.列表中时间数在 2~20000 之间。
    2.每个时间取值在 00:00~23:59 之间。
详见:https://leetcode.com/problems/minimum-time-difference/description/
C++:

class Solution {
public:
    int findMinDifference(vector<string>& timePoints)
    {
        int res = INT_MAX, n = timePoints.size(), diff = 0;
        sort(timePoints.begin(), timePoints.end());
        for (int i = 0; i < n; ++i)
        {
            string t1 = timePoints[i], t2 = timePoints[(i + 1) % n];
            int h1 = (t1[0] - ‘0‘) * 10 + t1[1] - ‘0‘;
            int m1 = (t1[3] - ‘0‘) * 10 + t1[4] - ‘0‘;
            int h2 = (t2[0] - ‘0‘) * 10 + t2[1] - ‘0‘;
            int m2 = (t2[3] - ‘0‘) * 10 + t2[4] - ‘0‘;
            diff = (h2 - h1) * 60 + (m2 - m1);
            if (i == n - 1)
            {
                diff += 24 * 60;
            }
            res = min(res, diff);
        }
        return res;
    }
};

参考:http://www.cnblogs.com/grandyang/p/6568398.html

原文地址:https://www.cnblogs.com/xidian2014/p/8910267.html

时间: 2024-08-30 03:59:45

539 Minimum Time Difference 最小时间差的相关文章

539. Minimum Time Difference

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

Python解Leetcode: 539. Minimum Time Difference

题目描述:给定一个由时间字符组成的列表,找出任意两个时间之间最小的差值. 思路: 把给定的链表排序,并且在排序的同时把60进制的时间转化成十进制整数: 遍历排序的数组,求出两个相邻值之间的差值: 求出首尾两个值之间的差值. class Solution(object): def findMinDifference(self, timePoints): """ :type timePoints: List[str] :rtype: int """

Leetcode-5197 Minimum Absolute Difference(最小绝对差)

1 #define _for(i,a,b) for(int i = (a);i < b;i ++) 2 #define _rep(i,a,b) for(int i = (a);i > b;i --) 3 #define INF 0x3f3f3f3f 4 #define MOD 1000000007 5 #define pb push_back 6 #define maxn 10003 7 8 class Solution 9 { 10 public: 11 vector<vector&l

Leetcode 530. Minimum Absolute Difference in BST

Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. Example: Input: 1 3 / 2 Output: 1 Explanation: The minimum absolute difference is 1, which is the difference between 2 and 1 (o

LeetCode Minimum Absolute Difference in BST

原题链接在这里:https://leetcode.com/problems/minimum-absolute-difference-in-bst/#/description 题目: Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. Example: Input: 1 3 / 2 Output: 1 Ex

Binary Search Tree-530. Minimum Absolute Difference in BST

Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. Example: Input: 1 3 / 2 Output: 1 Explanation: The minimum absolute difference is 1, which is the difference between 2 and 1 (o

530. Minimum Absolute Difference in BST

Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. Example: Input: 1 3 / 2 Output: 1 Explanation: The minimum absolute difference is 1, which is the difference between 2 and 1 (o

530. 平衡二叉树中,相邻节点的最小差 Minimum Absolute Difference in BST

Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. Example: Input: 1 3 / 2 Output: 1 Explanation: The minimum absolute difference is 1, which is the difference between 2 and 1 (o

[LeetCode] 530. Minimum Absolute Difference in BST Java

题目: Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. Example: Input: 1 3 / 2 Output: 1 Explanation: The minimum absolute difference is 1, which is the difference between 2 and