class Solution(object): def findComplement(self, num): """ :type num: int :rtype: int """ if num==0 or num==1: return 0 i=0 while 2**i<=num: if 2**(i+1)>num: return 2**(i+1)-num-1 else: i+=1
执行用时 :16 ms, 在所有 Python 提交中击败了92.62%的用户
内存消耗 :11.7 MB, 在所有 Python 提交中击败了35.95%的用户
执行用时为 8 ms 的范例 class Solution(object): def findComplement(self, num): """ :type num: int :rtype: int """ num=list(str(bin(num))[2::]) for i in range(0,len(num)): if num[i] =='0': num[i]='1' elif num[i]=='1': num[i]='0' num="".join(num) num=int(num,2) return(num)
——2019.10.10