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 ...
随机推荐
- 迭代加深 A* IDA* 初探
并没有有用的东西, 只是用来水的. 今天看搜索,想起来了A*和IDA* 看A*去了.... 啥玩意啊这是,他们代码为啥这么长??.... 看完了,...代码怎么写啊?? .....算了,直接看题吧 找 ...
- ++a和a++不是左值
上面的编译时会出现一下错误: aplus2.c:6:6: error: lvalue required as left operand of assignmentaplus2.c:7:6: error ...
- NTP 协议介绍
NTP协议 NTP(Network Time Protocol,网络时间协议)是由RFC 1305定义的时间同步协议,用来在分布式时间服务器和客户端之间进行时间同步.NTP基于UDP报文进行传输,使用 ...
- ICEM—两孔圆柱
原视频下载地址: https://pan.baidu.com/s/1eSJ7ciQ 密码: 1gj3
- 笔记七(编写第一个UEFI程序)
搭建好uefi开发环境之后,在MyWorkspace文件夹中建立一个文件夹ExamplePkg; ,然后在ExamplePkg文件夹中创建HelloWorld文件夹,Include文件夹,Exampl ...
- Linux设备驱动程序 之 后备高速缓存
设备驱动程序常常会反复的分配很多相同大小的内存块:内核实现了后备高速缓存来对此进行支持,以反复的使用这些相同的内存块: 创建 Linux内核的高速缓存管理有时被称为“slab分配器”:因此,相关函数和 ...
- 快速上手系列-C语言之指针篇(一)
快速上手系列-C语言之指针篇(一) 浊酒敬风尘 发布时间:18-06-2108:29 指针的灵活运用使得c语言更加强大,指针是C语言中十分重要的部分,可以说指针是C语言的灵魂.当然指针不是万能的,但没 ...
- [ML] Bayesian Logistic Regression
简单概率分类 Ref: 逻辑回归与朴素贝叶斯有什么区别? Ref: 机器学习笔记——逻辑回归(对数几率回归)和朴素贝叶斯分类器的对比 首先,搞清楚一个问题. naive bayes 能分类:逻辑回归也 ...
- MATLAB常用快捷键总结
MATLAB 命令栏显示处理的常用命令 清屏:clc 紧凑显示格式:format compact 宽松显示格式:format loose 数据高精度显示:format longG 数据低精度显示:fo ...
- iOS Xib布局某些控件显示或隐藏<约束的修改>
对于这个问题使用Masonry是很好解决的. 注意:绿色的是label2,当indexpath.section % 2 == 0时,label2不存在. 关键代码如下: if (indexPath.s ...