Concise and Clear Python Beats 100% (Greedy)

  • 0
    class Solution(object):
        def nextClosestTime(self, time):
            :type time: str
            :rtype: str
            a, b, _, c, d = time
            s = sorted(list(set([a, b, c, d])))
            # next closest time
            if d!=s[-1]:
                return a+b+':'+c+s[s.index(d)+1]
            if c!=s[-1]:
                if int(s[s.index(c)+1]+d)<=59:
                    return a+b+':'+s[s.index(c)+1]+s[0]
            if b!=s[-1]:
                if int(a+s[s.index(b)+1])<=23:
                    return a+s[s.index(b)+1]+':'+s[0]+s[0]
            if a!=s[-1]:
                if int(s[s.index(a)+1]+s[0])<=23:
                    return s[s.index(a)+1]+s[0]+':'+s[0]+s[0]
            # go to next day
            return s[0]+s[0]+':'+s[0]+s[0]

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.