338. Counting Bits

class Solution:
    def countBits(self, n: int) -> List[int]:
        a = [0]*(n+1)
        for i in range(1, n+1):
            if i % 2 == 0:
                a[i] = a[i//2]
            else:
                a[i] = a[i//2]+1
        return a
  • we know by math that any even number will have the same number of one bits as the number .
  • also know that the odd number will have one more bit than the number .
  • i have no idea why this works to be honest.