摘要: 1)转换二进制取不为零开始的字符串,补全反转输出。 class Solution: def reverseBits(self, n: int) -> int: t = bin(n)[2:] t = '0' * (32 - len(t)) + t return int(t[::-1],2) 2)移位, 阅读全文
posted @ 2021-06-11 15:15 泊鸽 阅读(80) 评论(0) 推荐(0)
摘要: 1)取异或,然后计算1的个数 class Solution: def hammingDistance(self, x: int, y: int) -> int: t = x^y count = 0 while t: count += 1 t = t& t-1 return count 阅读全文
posted @ 2021-06-11 14:50 泊鸽 阅读(46) 评论(0) 推荐(0)
摘要: 1)调库count class Solution: def hammingWeight(self, n: int) -> int: return bin(n).count('1') 2)按位与,n&n-1等于去掉倒数的第一个1 class Solution: def hammingWeight(se 阅读全文
posted @ 2021-06-11 13:55 泊鸽 阅读(93) 评论(0) 推荐(0)