Simple Python 38ms beats 100%


  • 0
    K
    def nextClosestTime(self, time):
        """
        :type time: str
        :rtype: str
        """
        t = [e for e in time if e!=':']
        a = sorted(t)
        for i in range(len(t)-1, -1, -1):
            if t[i] == a[-1]:
                t[i] = a[0]
            else:
                index_ti = a.index(t[i])
                index = index_ti + 1
                while a[index] == a[index_ti]:
                    index += 1
                if (i == 2 and int(a[index]) > 5) or (i==1 and int(t[0]+a[index]) > 23) or (i==0 and int(a[index]+t[1]) > 23):
                    t[i] = a[0]
                else:
                    t[i] = a[index]
                    break
        return t[0]+t[1]+':'+t[2]+t[3]

Log in to reply
 

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