Fetch the repository succeeded.
This action will force synchronization from 陌溪/LearningNotes, which will overwrite any changes that you have made since you forked the repository, and can not be recovered!!!
Synchronous operation will process in the background and will refresh the page when finishing processing. Please be patient.
一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。
这里我们需要做的就是,首先使用一个字典来进行存储,如果这个字典中原来有值了,那么我们就把这个字典删除,否则,按key为array[i],然后值设置成1,当我们遍历完成后,就会让剩下的数留下来,那么我们就通过遍历将他们查询出来返回即可。时间复杂度为 O(n)
# 一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。
class Solution:
# 返回[a,b] 其中ab是出现一次的两个数字
def FindNumsAppearOnce(self, array):
dict = {}
for i in range(len(array)):
if dict.get(array[i]) != None:
del dict[array[i]]
else:
dict[array[i]] = 1
if len(dict) == 0:
return None
list = []
for key in dict:
list.append(key)
return list[0], list[1]
if __name__ == '__main__':
array = [2,4,3,6,3,2,5,5]
print(Solution().FindNumsAppearOnce(array))
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。