Python using bit length


  • 0
    H

    def find_complement(num):
    if num == 0:
    return 1
    else:
    bit_length = int(math.floor(math.log(num,2.0)))+1
    max_number = math.pow(2, bit_length) - 1
    answer = max_number - num
    return int(answer)


Log in to reply
 

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