Skip to content

Latest commit

 

History

History
28 lines (25 loc) · 707 Bytes

401.md

File metadata and controls

28 lines (25 loc) · 707 Bytes

401. Binary Watch

Solution 1 (time O(1), space O(1))

class Solution(object):
    def readBinaryWatch(self, turnedOn):
        """
        :type turnedOn: int
        :rtype: List[str]
        """
        ans = []
        for i in range(12):
            for j in range(60):
                if self.count_ones(i) + self.count_ones(j) == turnedOn:
                    if j < 10:
                        ans.append(str(i) + ":0" + str(j))
                    else:
                        ans.append(str(i) + ":" + str(j))
        return ans
    
    def count_ones(self, num):
        ans = 0
        while num > 0:
            ans += num % 2
            num = num // 2
        return ans