当前位置:网站首页>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

边栏推荐
- 打新债 要求 打新债安全吗
- Advanced network accounting notes (IV)
- Ready to migrate to the cloud? Please accept this list of migration steps
- Rendering of kotlin jetpack compose tab using animatedvisibility
- 金九银十,靠这个细节,offer拿到手软!
- Convex optimization notes
- 35 year old crisis? It has become a synonym for programmers
- 墨天轮访谈 | IvorySQL王志斌—IvorySQL,一个基于PostgreSQL的兼容Oracle的开源数据库
- Is it safe to pay new debts
- 打新债好不好 打新债安全吗
猜你喜欢

Zabbix监控- Aruba AP运行数据

How long do you need to prepare for the PMP Exam?

Tupu software digital twin intelligent water service, breaking through the development dilemma of sponge City

Online text entity extraction capability helps applications analyze massive text data

IDEA控制台显示中文乱码

Netcf summary

Application de JDBC dans les essais de performance

Development of block hash quiz game system (DAPP)

Real topic of the 13th National Competition of single chip microcomputer in the Blue Bridge Cup

5 月最大的 GameFi 崩溃受害者能否在熊市中生存?| May Monthly Report
随机推荐
Helix QAC is updated to 2022.1 and will continue to provide high standard compliance coverage
Programmable, protocol independent software switch (read the paper)
19 classic cases of generator functions
35歲危機?內卷成程序員代名詞了…
20省市公布元宇宙路线图
Tupu software digital twin intelligent water service, breaking through the development dilemma of sponge City
Is it safe to make new debt
打新债 要求 打新债安全吗
解读2022年度敏捷教练行业现状报告
Online text entity extraction capability helps applications analyze massive text data
测试的重要性及目的
如何避免基因领域“黑天鹅”事件:一场预防性“召回”背后的安全保卫战
图扑软件数字孪生智慧水务,突破海绵城市发展困境
Advanced network accounting notes (III)
Leetcode daily question - 30 Concatenate substrings of all words
Activity registration | introduction to mongodb 5.0 sequential storage features
Official announcement. Net 7 preview 5
如何使用物联网低代码平台进行流程管理?
LeetCode 260. 只出现一次的数字 III
[one by one series] identityserver4 (II) using client credentials to protect API resources