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

边栏推荐
- Kotlin jetpack compose Tab的渲染 AnimatedVisibility的使用
- How long do you need to prepare for the PMP Exam?
- NAACL 2022 Findings | 字节提出MTG:多语言文本生成数据集
- Netcf summary
- 如何在Microsoft Exchange 2010中安装SSL证书
- Interview with Mo Tianlun | ivorysql wangzhibin - ivorysql, an Oracle compatible open source database based on PostgreSQL
- vs2022scanf函数的使用,使用scanf的报错-返回值被忽略:解决·方法
- Convex optimization notes
- 直播回顾 | 云原生混部系统 Koordinator 架构详解(附完整PPT)
- 如何使用物联网低代码平台进行流程管理?
猜你喜欢

LeetCode 1079. 活字印刷

SQL联合查询(内联、左联、右联、全联)的语法

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

函數的定義和函數的參數

Crise de 35 ans? Le volume intérieur est devenu synonyme de programmeur...

开源 SPL 重新定义 OLAP Server

Function definition and function parameters

Live sharing | Tencent cloud mongodb intelligent diagnosis and Performance Optimization Practice

Why is only one value displayed on your data graph?

为什么你的数据图谱分析图上只显示一个值?
随机推荐
5 月最大的 GameFi 崩溃受害者能否在熊市中生存?| May Monthly Report
GL Studio 5 安装与体验
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
How long do you need to prepare for the PMP Exam?
打新债 要求 打新债安全吗
金九银十,靠这个细节,offer拿到手软!
Helix QAC更新至2022.1版本,将持续提供高标准合规覆盖率
Real topic of the 13th National Competition of single chip microcomputer in the Blue Bridge Cup
LeetCode 每日一题——30. 串联所有单词的子串
The golden nine silver ten, depends on this detail, the offer obtains the soft hand!
Netseer: stream event telemetry notes for programmable data plane
打新债有条件吗 打新债安全吗
LeetCode 473. Match to square
宝安区航城街道领导一行莅临联诚发参观调研
Deep learning of handlebar handwriting (15): building your own corpus on hugging face
不止雷军 iQOO产品经理也称赞高通骁龙8+:焕然一新
Online text entity extraction capability helps applications analyze massive text data
Robust extraction of specific signals with time structure (Part 1)
好用的人事管理软件有哪些?人事管理系统软件排名!
Advanced network accounting notes (III)