• 题目描述:给定一个由时间字符组成的列表,找出任意两个时间之间最小的差值。

  • 思路:

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

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

  1. 【LeetCode】539. Minimum Time Difference 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/minimum-t ...

  2. LeetCode 530. Minimum Absolute Difference in BST (二叉搜索树中最小绝对差)

    Given a binary search tree with non-negative values, find the minimum absolute difference between va ...

  3. [Leetcode Week10]Minimum Time Difference

    Minimum Time Difference 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/minimum-time-difference/desc ...

  4. 51. leetcode 530. Minimum Absolute Difference in BST

    530. Minimum Absolute Difference in BST Given a binary search tree with non-negative values, find th ...

  5. LC 539. Minimum Time Difference

    Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minut ...

  6. 539. Minimum Time Difference

    Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minut ...

  7. Python 解LeetCode:671. Second Minimum Node In a Binary Tree

    题目在这里,要求一个二叉树的倒数第二个小的值.二叉树的特点是父节点的值会小于子节点的值,父节点要么没有子节点,要不左右孩子节点都有. 分析一下,根据定义,跟节点的值肯定是二叉树中最小的值,剩下的只需要 ...

  8. Python 解LeetCode:Intersection of Two Arrays

    最近,在用解决LeetCode问题的时候,做了349: Intersection of Two Arrays这个问题,就是求两个列表的交集.我这种弱鸡,第一种想法是把问题解决,而不是分析复杂度,于是写 ...

  9. Python 解leetcode:48. Rotate Image

    题目描述:把一个二维数组顺时针旋转90度: 思路: 对于数组每一圈进行旋转,使用m控制圈数: 每一圈的四个元素顺时针替换,可以直接使用Python的解包,使用k控制每一圈的具体元素: class So ...

随机推荐

  1. [Luogu] 最小差值生成树

    https://www.luogu.org/recordnew/show/6125570 思路就是巧妙的枚举所有的生成树,取最优值首先按照边权排序找出第一颗最小生成树(l, r),其中l表示最小边的编 ...

  2. openpyxl模块(excel操作)

    openpyxl模块介绍 openpyxl模块是一个读写Excel 2010文档的Python库,如果要处理更早格式的Excel文档,需要用到额外的库,openpyxl是一个比较综合的工具,能够同时读 ...

  3. spring相关知识点易错

    1.spring不支持静态变量注入,即不能再static变量上面加@Resoure 要是想注入的话就用set方法注入 private static ComboPooledDataSource pool ...

  4. Ubuntu start:未知任务:mysql

    在Ubuntu环境下,下载安装mysql但是到最后启动的时候却显示这一句话: start :未知任务:mysql 很纳闷,明明按照教程一步一步写的.后来才想起来,万能的方法,在前面加权限 sudo s ...

  5. 数据库groub by分组后,把多行数据合并成一行数据(Oracle、Postgres)

    关键字 row_number() over (partition by)   例如,下面的数据, 这是按照name分组后,展示property值. 我们想得到这样的值; 第一步:将每一组的proper ...

  6. 攻防世界Hello,CTF writeup

    解题过程 首先在ida中进行反汇编,查看main函数的代码: 代码的的36行处进行了一个字符串比较,如果v10的值等于v13的值会反馈一个success的输出.v13的值在第15行给出,因此需要知道v ...

  7. 五子棋AI教程

    https://github.com/Chuck-Ai/gobang 我写了非常详细的中文教程,教你如何一步步编写自己的五子棋AI: 五子棋AI设计教程第二版一:前言 五子棋AI设计教程第二版二:博弈 ...

  8. uWSGI Apache 处理 惊群效应的方式 现代的内核

    Serializing accept(), AKA Thundering Herd, AKA the Zeeg Problem — uWSGI 2.0 documentationhttps://uws ...

  9. apt如何列出所有已经安装的软件包

    apt如何列出所有已经安装的软件包 转 https://www.helplib.com/ubuntu/article_155294 问题: 我想将所有已安装软件包的列表输出到文本文件中,以便我可以查看 ...

  10. amm与tmpfs和swap

    /dev/shm对Oracle 11g的影响: ORACLE 从11g版本开始,引入了一个自动内存管理(Automatic Memory Management)特性,该特性需要更多的共享内存(/dev ...