Python解Leetcode: 539. Minimum Time Difference

  • 题目描述:给定一个由时间字符组成的列表,找出任意两个时间之间最小的差值。
  • 思路:
  1. 把给定的链表排序,并且在排序的同时把60进制的时间转化成十进制整数;
  2. 遍历排序的数组,求出两个相邻值之间的差值;
  3. 求出首尾两个值之间的差值。
class Solution(object):
    def findMinDifference(self, timePoints):
        """
        :type timePoints: List[str]
        :rtype: int
        """
        t = sorted(int(t[:2]) * 60 + int(t[-2:]) for t in timePoints)
        ret = 100000
        length = len(t)
        for i in range(length - 1):
            poor = t[i+1] - t[i]
            if poor < ret:
                ret = poor
        last = t[-1] - t[0] if t[-1]-t[0] <= 720 else 1440 - (t[-1]-t[0])
        ret = last if last < ret else ret
        return ret   

以上解决办法思路没问题,但是代码写出来不是很优,发现有大神写的,充分利用了Python的zip,很Pythonic,如下:

class Solution(object):
    def findMinDifference(self, timePoints):
        """
        :type timePoints: List[str]
        :rtype: int
        """
        t = sorted(int(t[:2]) * 60 + int(t[-2:]) for t in timePoints)
        t.append(t[0] + 1440)
        return min(b - a for a, b in zip(t, t[1:]))
时间: 2024-10-09 12:38:45

Python解Leetcode: 539. Minimum Time Difference的相关文章

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] 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

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:48. Rotate Image

题目描述:把一个二维数组顺时针旋转90度: 思路: 对于数组每一圈进行旋转,使用m控制圈数: 每一圈的四个元素顺时针替换,可以直接使用Python的解包,使用k控制每一圈的具体元素: class Solution(object): def rotate(self, matrix): """ :type matrix: List[List[int]] :rtype: void Do not return anything, modify matrix in-place inst

Python解Leetcode: 226. Invert Binary Tree

leetcode 226. Invert Binary Tree 倒置二叉树 思路:分别倒置左边和右边的结点,然后把根结点的左右指针分别指向右左倒置后返回的根结点. # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): d

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:

Python 解leetcode:3. Longest Substring Without Repeating Characters

题目描述:求一个字符串的不含重复字符的最长连续子串的长度: 思路: 使用一个哈希表保存字符出现的位置: 使用left和right分别表示子串的最左和最右字符的下标: 遍历字符串,如果当前字符在哈希表中并且当前字符在哈希中的位置大于left,表明left和right之间存在和right索引上相同的字符,此时把left置为当前值在哈希表中的值: 把当前索引值+1,表示是第几个字符,求出当前最大长度: 3-4步重复,直到获得遍历完成: 感觉这是个动态规划题,暂时没用动态规划分析,后续再说. class

Python 解leetcode:49. Group Anagrams

题目描述:给出一个由字符串组成的数组,把数组中字符串的组成字母相同的部分放在一个数组中,并把组合后的数组输出: 思路: 使用一个字典,键为数组中字符串排序后的部分,值为排序后相同的字符串组成的列表: 遍历数组完成后,返回字典的值就可以了. class Solution(object): def groupAnagrams(self, strs): """ :type strs: List[str] :rtype: List[List[str]] ""&qu

Python 解LeetCode:23. Merge k Sorted Lists

题目描述:把k个排序的链表组成的列表合并成一个排序的链表 思路: 使用堆排序,遍历列表,把每个列表中链表的头指针的值和头指针本身作为一个元素放在堆中: 第一步中遍历完列表后,此时堆中最多会有n个元素,n是列表的长度: 当堆不为空,取出堆中的最小值,然后把该值的指针指向下一个元素,并入堆: 第3步可以确保堆永远是o(n)大小的: 堆为空返回头结点就可以了 # Definition for singly-linked list. # class ListNode(object): # def __i