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

边栏推荐
- 好用的人事管理软件有哪些?人事管理系统软件排名!
- Function definition and function parameters
- Development of block hash quiz game system (DAPP)
- 八大误区,逐个击破(终篇):云难以扩展、定制性差,还会让管理员失去控制权?
- Is it safe to pay new debts
- Helix QAC更新至2022.1版本,将持续提供高标准合规覆盖率
- 20 provinces and cities announce the road map of the meta universe
- Are internal consultants and external consultants in SAP implementation projects difficult or successful?
- 基于SSM实现微博系统
- Gaussdb (DWS) database intelligent monitoring operation and maintenance service - node monitoring indicators
猜你喜欢

游戏资产复用:更快找到所需游戏资产的新方法

LeetCode 260. 只出现一次的数字 III

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

Summary of accelerating mobile applications at network edge with software programmable FPGA

Zabbix监控- Aruba AP运行数据

金九银十,靠这个细节,offer拿到手软!

Programmable, protocol independent software switch (read the paper)

Kubernetes 资源拓扑感知调度优化

为什么你的数据图谱分析图上只显示一个值?

Interpreting the 2022 agile coaching industry status report
随机推荐
GL Studio 5 installation and experience
Netseer: stream event telemetry notes for programmable data plane
20set introduction and API
Advanced network accounting notes (VII)
Leaders of Hangcheng street, Bao'an District and their delegation visited lianchengfa for investigation
基于SSM实现微博系统
LeetCode 260. 只出现一次的数字 III
MySQL时间函数的运用,简单问题
开源 SPL 重新定义 OLAP Server
【云动向】华为云云商店品牌全新发布 4大亮点都在这儿
Is it safe to pay new debts
SAP实施项目上的内部顾问与外部顾问,相互为难还是相互成就?
技术分享| WVP+ZLMediaKit实现摄像头GB28181推流播放
Summary of accelerating mobile applications at network edge with software programmable FPGA
金九银十,靠这个细节,offer拿到手软!
JDBC 在性能測試中的應用
Advanced network planning notes (IX)
宝安区航城街道领导一行莅临联诚发参观调研
金鱼哥RHCA回忆录:DO447管理用户和团队的访问--用团队有效地管理用户
SAVE: 软件分析验证和测试平台