1720. 解码异或后的数组
class Solution:
def decode(self, encoded: List[int], first: int) -> List[int]:
'''
例子:
101^011 = 110 101^110 = 011
a^b^b = a 异或的性质
'''
arr = [first]
for i in range(len(encoded)):
arr.append(arr[-1]^encoded[i])
return arr
190. 颠倒二进制位
法1:位运算,循环遍历
class Solution:
def reverseBits(self, n: int) -> int:
#位运算
res = 0
for i in range(32):
res = (res << 1) | (n & 1)
n>>=1
return res
法2:转换为字符串进行操作
class Solution:
def reverseBits(self, n: int) -> int:
n = bin(n)[2::][::-1]
for i in range(32-len(n)):
n += "0"
return int(n,2)