都是十进制和二进制的转换
461)
class Solution(object):
def hammingDistance(self, x, y):
"""
:type x: int
:type y: int
:rtype: int
"""
count=0
a=[]
shang,yushu=divmod(x,2)
while shang!=0:
a.append(yushu)
shang,yushu=divmod(shang,2)
a.append(yushu)
b=[]
shang,yushu=divmod(y,2)
while shang!=0:
b.append(yushu)
shang,yushu=divmod(shang,2)
b.append(yushu)
l1=len(a)
l2=len(b)
if l1<l2:
t=l1
while t<l2:
a.append(0)
t+=1
elif l1>l2:
t=l2
while t<l1:
b.append(0)
t+=1
l=len(a)-1
while l>=0:
if a[l]!=b[l]:
count+=1
l=l-1
return count
if __name__=="__main__":
sol=Solution()
print sol.hammingDistance(4,1)
476)
class Solution(object):
def findComplement(self, num):
"""
:type num: int
:rtype: int
"""
a=[]
b=[]
shang,yushu=divmod(num,2)
while shang!=0:
a.append(yushu)
shang,yushu=divmod(shang,2)
a.append(yushu)
i=len(a)-1
j=0
while i>=0:
if a[i]==1:
b.append(0)
else:
b.append(1)
j+=1
i=i-1
res=0
i=0
while i<len(b):
zhishu=2**(len(b)-1-i)
res+=b[i]*zhishu
i=i+1
return res
if __name__=="__main__":
sol=Solution()
print sol.findComplement(5)