Count number of 1's in binary representation

后端 未结 21 2058
天涯浪人
天涯浪人 2020-11-28 01:32

Efficient way to count number of 1s in the binary representation of a number in O(1) if you have enough memory to play with. This is an interview question I found on an onli

21条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-28 02:03

    In python or any other convert to bin string then split it with '0' to get rid of 0's then combine and get the length.

    len(''.join(str(bin(122011)).split('0')))-1
    

提交回复
热议问题