扫码关注官方订阅号
走同样的路,发现不同的人生
利用两点:
0按位异或a=a
a按位异或a=0所以如下,复杂度是o(n)
def find(arr): tmp = 0 for item in arr: tmp ^= item return tmp if __name__ == '__main__': arr = [1,2,3,2,1,2,3,2,4]
时间复杂度O(n):
# coding: utf-8 from collections import defaultdict def find_odds(arr): odds = defaultdict(int) for item in arr: odds[item] += 1 if odds[item] > 1: del odds[item] return odds.keys() arr = [5,1,2,3,2,1,2,3,2,4] odds = find_odds(arr)
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
扫描下载App
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
利用两点:
0按位异或a=a
a按位异或a=0
所以如下,复杂度是o(n)
时间复杂度O(n):