190. Reverse Bits

来源:互联网 发布:java parseint方法 编辑:程序博客网 时间:2024/06/16 17:33

Reverse bits of a given 32 bits unsigned integer.

For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000).

Follow up:
If this function is called many times, how would you optimize it?

Related problem: Reverse Integer

class Solution(object):  def reverseBits(self,n):    li = [0] * 32    count = 0    num = 0    while(n!=0):      li[31-count] = n%2      n = n//2      count+=1    li.reverse()    for i in range(32):      num += pow(2,31-i) * li[i]    return num


原创粉丝点击