1 minute read

400. Nth Digit (medium)

class Solution:
    def findNthDigit(self, n: int) -> int:
        digit = 1
        temp = 9
        while n > temp * digit:
            n -= temp * digit
            temp *= 10
            digit += 1

        n -= 1
        a, b = n // digit, n % digit
        # print(n, a, b)
        # print(10 ** (digit - 1) + a)        
        return int(str(10 ** (digit - 1) + a)[b])

401. Binary Watch (easy)

class Solution:
    def readBinaryWatch(self, turnedOn: int) -> List[str]:
        res = []
        for h in range(12):
            for m in range(60):
                if bin(h).count('1') + bin(m).count('1') == turnedOn:
                    res.append(f"{h}:{m:02d}")
        return res

404. Sum of Left Leaves (easy)

class Solution:
    def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:

        def dfs(cur, go_left):
            if not cur:
                return 0
            
            if not cur.left and not cur.right:
                return cur.val if go_left else 0
            
            temp = 0
            temp += dfs(cur.left, True)
            temp += dfs(cur.right, False)
            return temp

        return dfs(root, False)

405. Convert a Number to Hexadecimal (easy)

class Solution:
    def toHex(self, num: int) -> str:
        if num == 0:
            return '0'
        neg = num < 0
        if neg:
            num += 2 ** 32
        
        d = [str(x) for x in range(10)]
        d += ['a', 'b', 'c', 'd', 'e', 'f']

        res = ''
        while num:
            res += d[num % 16]
            num //= 16
        
        return res[::-1]