当前位置:网站首页>Leetcode: Sword finger offer 56 - I. number of occurrences in the array [grouping XOR]
Leetcode: Sword finger offer 56 - I. number of occurrences in the array [grouping XOR]
2022-06-09 18:49:00 【Review of the white speed Dragon King】

analysis
If it all happens twice , Only one appears at a time
Then the one that appears at a time is all XOR
But there are two things that happen at once , So these two goals are all exclusive or a and b Exclusive or of res
We find res One of them is 1 To distinguish a and b
Then the other same words will definitely be divided into the same group
In this way, the grouping XOR obtains two results, that is, the two appear 1 The number of times
ac code
class Solution:
def singleNumbers(self, nums: List[int]) -> List[int]:
# Group XOR
# reduce(func, list)
res = reduce(lambda x, y: x ^ y, nums)
# find a bit to divide these two nums
div = 1
while div & res == 0:
div <<= 1
a, b = 0, 0
# two groups and xor
for num in nums:
if num & div:
a ^= num
else:
b ^= num
return [a, b]
summary
Several even numbers , An odd number appears , Eliminate even occurrences of by XOR ( Magic XOR )
Pay attention to the reduce Usage of :reduce(func, list)
边栏推荐
- mfc连接数据库显示未发现数据源名称并且未指定默认驱动程序
- Inspur cs5280h raid scheme details
- Win10安装WSL1在D、E、F盘
- Function: multi file upload and unified submission
- 示波器电流探头的消磁与平衡调节步骤
- Some software I usually use
- 技术分享 | Selenium多浏览器处理
- Alibaba open source TTL is used in microservices to gracefully realize inter thread reuse of identity information
- 聊聊 MQ 技术选型
- mysql数据库角色如何创建
猜你喜欢

【SOLIDWORKS-详细记录】解决默认模板无效问题及添加自定义模板

Will quic become a disruptor of Internet transmission?

Loop structure programming 2

2022 SME Digital Forum held 360 Enterprise Security Cloud release new upgrade

How to create MySQL database roles

Pcap file

数据库:高并发下的数据字段变更

为什么云原生数据湖值得关注?

什么是集群?为什么要使用集群架构?

2022年开什么实体店比较赚钱?适合女性做的小成本开店,叶其芳大健康
随机推荐
有源差分探头在USB2.0一致性分析测试的准备工作
gcc编译demo+Makefile使用
R|mapping. seq()
Dl|part of cyclic neural network
C# 30. String interception
STM32内存知识
技术分享 | Selenium多浏览器处理
[SolidWorks detailed records] operation records of measuring method, setting the automatic elevation datum plane of sketch, setting the zoom in and zoom out direction of roller, adding thread line to
GCC compile demo+makefile use
金鱼哥RHCA回忆录:DO447管理清单--编写YAML清单文件
【SOLIDWORKS-详细记录】测量方法、设置草图自动正视基准面、设置滚轮放大缩小方向、螺丝添加螺纹线等操作记录
一些我平常用到的软件
为什么云原生数据湖值得关注?
155_ Model_ Safety stock of power Bi & power pivot purchase, sales and inventory
MySQL parallel replication (MTS) principle (full version)
上位机开发(开篇)
图解|高性能服务器设计之缓存系统一致性
Details on how Tencent can reduce the live broadcast delay by more than 90%
Ad delete dimension
Ad PCB drawing transparency