当前位置:网站首页>32 < tag array and bit operation > supplement: Lt. sword finger offer 56 - I. number of occurrences of numbers in the array
32 < tag array and bit operation > supplement: Lt. sword finger offer 56 - I. number of occurrences of numbers in the array
2022-07-23 10:28:00 【Caicai's big data development path】
lt. The finger of the sword Offer 56 - I. The number of occurrences of numbers in an array
[ Case needs ]

[ Thought analysis ]

[ Code implementation ]
class Solution {
public int[] singleNumbers(int[] nums) {
// Because the same number XOR is 0, Any number and 0 The XOR result is itself .
// Therefore, the final result of traversing the XOR array is the result of two digital XORs that appear only once : namely z = x ^ y
int z = 0;
for(int i : nums) z ^= i;
// According to the nature of XOR, we can know :z At least one of them is 1, otherwise x And y It's equal .
// We use an auxiliary variable m To preserve z Which one of them is 1.( There may be more than one bit for 1, We found the lowest 1 that will do ).
// for instance :z = 10 ^ 2 = 1010 ^ 0010 = 1000, The fourth is 1.
// We will m Initialize to 1, If (z & m) The result is equal to 0 explain z The minimum is 0
// Every time we will m Move left one bit and follow z Do and operate , Until the result is not 0.
// here m It should be equal to 1000, Same as z equally , The fourth is 1.
int m = 1;
while((z & m) == 0) m <<= 1;
// We traverse arrays , Follow each number with m Conduct and operate , The result is 0 As a group , The result is not 0 As a group
// For example, for arrays :[1,2,10,4,1,4,3,3], Let's follow each number with 1000 Do and operate , It can be divided into the following two groups :
//nums1 The storage result is 0 Of : [1, 2, 4, 1, 4, 3, 3]
//nums2 The storage result is not 0 Of : [10] ( Happen nums2 Only one of them 10, If the number in the original array were larger, it wouldn't be like this )
// At this point, we find that the problem has degenerated into that a number in the array only appears once
// Respectively for nums1 and nums2 Traversing XOR will get what we expect x and y
int x = 0, y = 0;
for(int i : nums) {
// Here we are through if...else take nums Divided into two groups , While traversing XOR .
// Create two arrays with us nums1 and nums2 The principle is the same .
if((i & m) == 0) x ^= i;
else y ^= i;
}
return new int[]{
x, y};
}
}
边栏推荐
- redis的事务、锁机制、秒杀
- ARP Spoofing protection of network security
- Special training - linked list
- 适合拼多多小商家配件的一些思路跟技巧
- MySQL queries all table names and column information of the database through SQL
- Self organization is the two-way rush of managers and members
- [basics of C language] 14 file, declaration and format input and output
- 客户至上 | 国产BI领跑者,思迈特软件完成C轮融资
- 金仓数据库 KingbaseES SQL 语言参考手册 (8. 函数(二))
- Redis安装
猜你喜欢

Reverse theoretical knowledge 1

How to improve browsing security? Teach you to set up a secure browser trust site

网络数据泄露事件频发,个人隐私信息如何保护?

mysql三表查询问题

Three goals and eight tasks of intelligent construction pilot city notice

Redis事务-秒杀案例模拟实现详细过程

vs中新建文件/筛选器/文件夹

ssm框架外卖订餐系统

SSH超市进销存管理系统

What is the experience of writing concurrent tool classes (semaphore, cyclicbarrier, countdownlatch) by yourself in line 30?
随机推荐
[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (VII)
MySQL queries all table names and column information of the database through SQL
宇视NVR设备接入EasyCVR平台,离线后无法上线该如何解决?
隐适美invisAlign口扫转诊方式(导出口扫数据+线上问诊)
2022/7/20
科技赋能新保险:中华财险的数字化转型
金仓数据库 KingbaseES SQL 语言参考手册 (8. 函数(八))
2. Judgment statement
Three goals and eight tasks of intelligent construction pilot city notice
Unity Image中Sprite和overrideSprite区别(转载)
Self organization is the two-way rush of managers and members
Kingbasees SQL language reference manual of Jincang database (8. Function (I))
Reverse theoretical knowledge 1
【代码案例】网页版表白墙 & 待办事项 (包含完整源码)
8.< tag-动态规划和LCS问题>lt.300. 最长递增子序列 + lt.674. 最长连续递增序列
[basics of C language] 14 file, declaration and format input and output
【汇总篇】
腾讯云客户端命令行工具tccli主流程解析
金仓数据库 KingbaseES SQL 语言参考手册 (4. 伪列)
Kingbasees SQL language reference manual of Jincang database (8. Function (6))