Python solution leetcode:539. Minimum Time Difference

Source: Internet
Author: User

    • Title Description: Given a list of time characters, find the smallest difference between any two times.

    • Ideas:
    1. Sort the given list and convert the time of 60 into decimal integer while sorting;
    2. Traversing the sorted array, the difference between the two adjacent values is calculated;
    3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.