当前位置:网站首页>LeetCode 260. Number III that appears only once
LeetCode 260. Number III that appears only once
2022-06-23 19:43:00 【51CTO】
So far I've written 500 Multiple algorithm problems , Some of them have been sorted into pdf file , At present, there are 1000 Multi page ( And it will continue to increase ), You can download it for free

Bit operation solution
Reference resources 《494, A number in which the solution of a bit operation occurs only once 》

We see the XOR result on the far right 1, That's the red part , According to this position is 0 still 1 Divide the original array into two groups , that 13 and 17 Definitely not in the same group . So each group becomes a single number , The other numbers appear twice . Then we can use 《494, A number in which the solution of a bit operation occurs only once 》 The way to solve . The code is as follows
public
int[]
singleNumber(
int[]
nums) {
int
bitmask
=
0;
// XOR all the elements in the array
for (
int
num :
nums) {
bitmask
^
=
num;
}
// Because the result of XOR operation is not always 2 Of n The next power ,
// There may be more than one in binary 1, For the sake of calculation
// We just need to keep any of them 1, Everything else is
// Let him become 0, What's left here is the one on the far right 1
bitmask
&=
-
bitmask;
int[]
rets
= {
0,
0};
for (
int
num :
nums) {
// Then divide the array into two parts , Each part is in
// Or, respectively
if ((
num
&
bitmask)
==
0) {
rets[
0]
^
=
num;
}
else {
rets[
1]
^
=
num;
}
}
return
rets;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
The bit operations above bitmask &= -bitmask; It means to put bitmask The rightmost binary 1 Retain , All the other positions are changed to 0, Just find a data and print it

Let's look at the result of the calculation

边栏推荐
- 80% of people will be wrong about the three counter intuitive questions?
- Matrix analysis notes (II)
- 图扑软件数字孪生智慧水务,突破海绵城市发展困境
- [cloud trends] the four highlights of Huawei cloud store brand new release are here
- 【云动向】华为云云商店品牌全新发布 4大亮点都在这儿
- QGIS import WMS or WMTs
- 基于 ShardingSphere 的得物数据库中间件平台“彩虹桥”演进之路
- logstash启动 -r 参数
- What are the requirements for new bonds? Is it safe to play new bonds
- 如何使用物联网低代码平台进行流程管理?
猜你喜欢

Netcf summary

Save: software analysis, verification and test platform

Ready to migrate to the cloud? Please accept this list of migration steps

Kubernetes resource topology aware scheduling optimization

增加双因素认证,不惧密码泄露,更不惧123456

LeetCode 每日一题——30. 串联所有单词的子串

IDEA控制台显示中文乱码

测试的重要性及目的

35歲危機?內卷成程序員代名詞了…

每日刷题记录 (二)
随机推荐
Rendering of kotlin jetpack compose tab using animatedvisibility
Save: software analysis, verification and test platform
Hardware development notes (6): basic process of hardware development, making a USB to RS232 module (5): creating USB package library and associating principle graphic devices
Uniswap创始人:不会为Genie发行独立代币,Genie产品将集成至Uniswap界面
20省市公布元宇宙路线图
Elastricearch's fragmentation principle of the second bullet
What conditions do you need to meet to fight new debts? Is it safe to fight new debts
活动报名 | MongoDB 5.0 时序存储特性介绍
The golden nine silver ten, depends on this detail, the offer obtains the soft hand!
Programmable, protocol independent software switch (read the paper)
打新债有何要求 打新债安全吗
IDEA控制台显示中文乱码
Check four WiFi encryption standards: WEP, WPA, WPA2 and WPA3
How to write a great online user manual in 7 steps
Function definition and function parameters
游戏资产复用:更快找到所需游戏资产的新方法
Gaussdb (DWS) database intelligent monitoring operation and maintenance service - node monitoring indicators
Development notes of wedding studio applet based on wechat applet
Advanced network accounting notes (III)
Helix QAC is updated to 2022.1 and will continue to provide high standard compliance coverage