- Sort the given list and convert the time of 60 into decimal integer while sorting;
- Traversing the sorted array, the difference between the two adjacent values is calculated;
- Find the difference between the first two values.
classSolution (Object):defFindmindifference ( Self, timepoints):""": Type TIMEPOINTS:LIST[STR]: Rtype:int """T= Sorted(int(t[:2])* - + int(t[-2:]) forTinchtimepoints) ret= 100000Length= Len(t) forIinch Range(length- 1): Poor=T[i+1]-T[i]ifPoor<Ret:ret=Poor last=t[-1]-t[0]ift[-1]-t[0]<= 720 Else 1440 -(t[-1]-t[0]) ret=LastifLast<RetElseRetreturnRet
The above solution is not a problem, but the code is not very good, found that the great God wrote, the full use of the Python zip, very pythonic, as follows:
classSolution (Object):defFindmindifference ( Self, timepoints):""": Type TIMEPOINTS:LIST[STR]: Rtype:int """T= Sorted(int(t[:2])* - + int(t[-2:]) forTinchtimepoints) T.append (t[0]+ 1440)return min(b-A forA, binch Zip(T, t[1:]))
Python solution leetcode:539. Minimum Time Difference