who can tell me where went wrong?


  • 0
    C
    import math
    class Solution(object):
     result_x = [0]*31   
     result_y = [0]*31
     highest_x = 0
     highest_y = 0
     result = 0
     def hammingDistance(self, x, y):
         if x > 0:
             self.highest_x = math.floor(math.log(x, 2))
             self.result_x[int(31-self.highest_x-1)] = 1
             temp_x = x - 2**self.highest_x
         else:
             temp_x = 0
         if y > 0:
             self.highest_y = math.floor(math.log(y, 2))
             self.result_y[int(31-self.highest_y-1)] = 1
             temp_y = y - 2**self.highest_y 
         else:
             temp_y = 0
         
         if temp_x == 0 and temp_y == 0:
             for i in range(31):
                 if self.result_x[i] != self.result_y[i]:
                     self.result += 1
             return self.result
                
         self.hammingDistance(temp_x, temp_y)
     
    
    

    0_1504068458245_屏幕快照 2017-08-30 下午12.45.58.png

    First , I get binary code of number and put it into a int list, then i compare two list and find where has different. It is effective on my ide(just as image shows) when we test case (x=3, y=1), but leetcode return a null???

    so who can tell me where went wrong about my code??

    Thanks!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.