Python Backtracking Solution


  • 0
    class Solution(object):
        def backtrack(self, r, h, num, ind):
            if num == 0:
                hour, minute = 0, 0
                for i in xrange(4):
                    hour = hour*2 + h[i]
                for i in xrange(4, 10):
                    minute = minute*2 + h[i]
                if hour < 12 and minute < 60:
                    r.append('%d:%02d' % (hour, minute))
            else:
                for i in xrange(ind, 11-num):
                    h[i] = 1
                    self.backtrack(r, h, num-1, i+1)
                    h[i] = 0
        
        def readBinaryWatch(self, num):
            r = []
            self.backtrack(r, [0]*10, num, 0)
            return r
    

Log in to reply
 

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