最优解
如果注意到除了一个元素,其他元素有且只有两个,于是可以想到异或算法。异或算法如果是两个一样的话,为0。0和数字异或,为其本身。并且异或具备交换律与结合率。
时间复杂度为n,空间复杂度为1。
暴力求解
时间复杂度为n^2,我觉得空间复杂度仍为1,因为没有额外存储数据。
python读取一串由字符断开的数据,并将其分别存储在列表中的方法。
https://blog.csdn.net/qq_43503724/article/details/95352575?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase.
代码实现:
int 需要对字符串操作或者单个字节,而不能对整个列表操作。
str=input("请输入一串数字")
str=str.split(" ")
for i in range(len(str)):
str[i]=int(str[i])
#print(str)
for i in range(len(str)):
lamble=False
for j in range(i+1,len(str)):
if str[i]==str[j]:
lamble=True
for j in range(0,i):
if str[i]==str[j]:
lamble=True
if lamble==False:
print("唯一的数字为:",str[i])