要求将算法1~6设计为3. 3. 4节定义的顺序表Dy
要求将算法1~6设计为3. 3. 4节定义的顺序表DynamicArrayList类的方法。
5.设在某顺序表中,有一个整数在该表中的出现次数为奇数,其余整数的出现次数均为偶数。设计尽可能高效的算法,寻找出现次数为奇数的整数。例如:(1, 2. 5, 2,5. 1. 5)中,出现次数为奇数的整数为5
答案
class DynamicArrayList: def __init__(self): self.data = [] def find_odd_times(self): """寻找出现次数为奇数的整数,异或法""" res = 0 for num in self.data: res ^= num return res