question about missing element in array

后端 未结 2 1093
庸人自扰
庸人自扰 2020-12-30 18:17

i have following problem from book introduction algorithm second edition by MIT university

problem is following

An array A[1 . . n] contai

2条回答
  •  感情败类
    2020-12-30 18:53

    Here is a Python implementation:

    def bit_at(n, bit):
        return (n>>bit) & 1
    
    def find_missing(a, bits):
    
        indexes = range(len(a))
        missing = 0
    
        for bit in range(bits):
    
            ones = [i for i in indexes if bit_at(a[i], bit)==1]
            zeroes = [i for i in indexes if bit_at(a[i], bit)==0]
    
            if len(ones) <= len(zeroes):
                indexes = ones
                missing |= (1<

提交回复
热议问题