Python解Leetcode: 539. Minimum Time Difference
题目描述:给定一个由时间字符组成的列表,找出任意两个时间之间最小的差值。
思路:
- 把给定的链表排序,并且在排序的同时把60进制的时间转化成十进制整数;
- 遍历排序的数组,求出两个相邻值之间的差值;
- 求出首尾两个值之间的差值。
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的更多相关文章
- 【LeetCode】539. Minimum Time Difference 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/minimum-t ...
- LeetCode 530. Minimum Absolute Difference in BST (二叉搜索树中最小绝对差)
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- [Leetcode Week10]Minimum Time Difference
Minimum Time Difference 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/minimum-time-difference/desc ...
- 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 ...
- LC 539. Minimum Time Difference
Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minut ...
- 539. Minimum Time Difference
Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minut ...
- Python 解LeetCode:671. Second Minimum Node In a Binary Tree
题目在这里,要求一个二叉树的倒数第二个小的值.二叉树的特点是父节点的值会小于子节点的值,父节点要么没有子节点,要不左右孩子节点都有. 分析一下,根据定义,跟节点的值肯定是二叉树中最小的值,剩下的只需要 ...
- Python 解LeetCode:Intersection of Two Arrays
最近,在用解决LeetCode问题的时候,做了349: Intersection of Two Arrays这个问题,就是求两个列表的交集.我这种弱鸡,第一种想法是把问题解决,而不是分析复杂度,于是写 ...
- Python 解leetcode:48. Rotate Image
题目描述:把一个二维数组顺时针旋转90度: 思路: 对于数组每一圈进行旋转,使用m控制圈数: 每一圈的四个元素顺时针替换,可以直接使用Python的解包,使用k控制每一圈的具体元素: class So ...
随机推荐
- rep stos ptr dword es:[edi]
今天读代码时,忽然跳出如下一条指令==>>汇编代码: rep stos dword ptr es:[edi] 在网上查了相关资料显示:/************************** ...
- alarm函数
alarm函数 设置定时器(闹钟).在指定seconds后,内核会给当前进程发送14)SIGALRM信号.进程收到该信号,默认动作终止. 每个进程都有且只有唯一个定时器. unsigned int a ...
- [报错解决] 关于windows下 使用Anaconda3安装的python无法使用ssl模块问题.关联pip无法下载https
关联错误: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not ...
- 【做题记录】Codeforces做题记录
最近决定写一些CF Div.1的题,练习一下速度和代码能力. 暂定从中考后的Codeforces Round #572开始. 大部分比较简单的题直接把题解写在这里,不单独开文章了. Codeforce ...
- pwn学习日记Day6 基础知识积累
知识杂项 ELF:在计算机科学中,是一种用于二进制文件.可执行文件.目标代码.共享库和核心转储格式文件. char fgets(char buf, int bufsize, FILE stream); ...
- SQLyog Enterprise Trial 试用期问题
SQLyog Enterprise Trial 是 SQLyog的试用版,有效期30天:试用期过后提示购买之后才能使用:解决办法:修改注册表(过期就得改比较麻烦,但暂时可以用,等有时间了再找其他办法) ...
- cmder的segmentation fault错误修复
Segmentation fault 现场还原 问题出现的原因是我在 cmder的命令行里执行了cmder /register ALL命令,本意是把cmder放到右键菜单里去的 但我没想到的是,各种不 ...
- linux下如何批量替换多个文件中的某个字符串?
答: sed -i "s/<old_string>/<new_string>/g" `grep "<old_string>" ...
- context_processor 上下文处理器
context_processor 上下文处理器 博客里面有三个地方用到了标签云:主页面,分类页面,博客详情页面,于是有了下面一段代码 # 主页面 @main.route("/") ...
- Build Telemetry for Distributed Services之OpenTracing项目
中文文档地址:https://wu-sheng.gitbooks.io/opentracing-io/content/pages/quick-start.html 中文github地址:https:/ ...