simple python solution


  • 0
    Y
        def nextClosestTime(self, time):
            """
            :type time: str
            :rtype: str
            """
            q=[]
            digit=time.split(":")
            t=int(digit[0])*1.0+int(digit[1])/100.0
            for c in time:
                if c!=":":
                    q.append(int(c))
                    
            hours=[]
            minutes=[]
            for i in xrange(len(q)):
                if q[i]<3:
                    h1=q[i]*10
                    for j in xrange(len(q)):
                        if q[i]<2:
                            hours.append(h1+q[j])
                        else:
                            if q[j]<4:
                                hours.append(h1+q[j])
    
            for i in xrange(len(q)):
                if q[i]<6:
                    m1=q[i]*10
                    for j in xrange(len(q)):
                            minutes.append(m1+q[j])
                            
            diff=10000.0     
            hh,mm=-1,-1
            for h in hours:
                for m in minutes:
                    x=h*1.0+m/100.0
                    if x==t:continue
                    delt=24.0+x-t if x<t else x-t
                    if diff>delt:
                        diff,hh,mm=delt,h,m
                          
            if hh==-1 and mm==-1:
                return time
            strh="0"+str(hh) if hh<10 else str(hh)
            strm="0"+str(mm) if mm<10 else str(mm)
            ans=strh+":"+strm
            return ans
    

Log in to reply
 

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