位运算 1,只出现一次的数字(136) 采用位运算中的异或运算,异或运算满足交换律与结合律 ^ class Solution ( object ) : def singleNumber ( self , nums ) : """ :type nums: List[int] :rtype: int """ res = 0 for i in nums : res ^ = i return res 2,颠倒二进制位(190) 采用位运算中的右移>>与左移<<操作 class Solution : # @param n, an integer # @return an integer def reverseBits ( self , n ) : res = 0 count = 32 while count : res << = 1 res += n & 1 n >> = 1 count -= 1 return int ( bin ( res ) , 2 ) 3,位1的个数(191) 采用位运算中的&操作 num&(num-1)可以消去num末尾的0 class Solution ( object ) : def hammingWeight ( self , n ) : """ :type n: int :rtype: int """ res = 0 while n : n = n & (