当前位置:网站首页>The number of occurrences of numbers in the offer 56 array (XOR)
The number of occurrences of numbers in the offer 56 array (XOR)
2022-07-05 07:24:00 【BugMaker-shen】
List of articles
The number of occurrences of numbers in an array I

The finger of the sword Offer 56 - I. The number of occurrences of numbers in an array
The same number is XOR 0, Different XORs are 1,0 And any number is XOR equal to the number itself
therefore , All numbers in the array are XOR = Target two numbers XOR . Because these two numbers are different , So the XOR result must not be 0
Suppose the binary result of array XOR is 10010, Then it means that these two targets number from right to left 2 position bit Is different
Then you can use the second digit of all the numbers in the array bit by 0 perhaps 1 Divide the array into 2 individual
In this way, the target number must be scattered in different arrays , And the same number must fall in the same array
The numbers in these two arrays are XOR respectively , The result is the answer
class Solution {
public:
vector<int> singleNumbers(vector<int>& nums) {
int res = 0;
for(int val : nums){
res ^= val;
}
int index = 0;
while((res & 1) == 0){
// This loop is used to find which of the two target numbers bit Different , Use this bit Separate the two target numbers
// One of the XOR results bit by 1, Then it means that the two target numbers should bit Different
res >>= 1;
index++;
}
int num1 = 0;
int num2 = 0;
for(int val : nums){
// According to one bit Whether to group the same
if(((val >> index) & 1) == 1){
num1 ^= val;
}else{
num2 ^= val;
}
}
return {
num1, num2};
}
};
The number of occurrences of numbers in an array II
The number of occurrences of numbers in an array II

1. Method 1 : An operation
0 ^ x = x
x ^ x = 0
x & ~x = 0
x & ~0 = x
In limine a = 0,b = 0
x First appearance :a = (a ^ x) & ~b As the result of the a = x, At this time because a = x 了 ,b = (b ^ x) & ~a As the result of the b = 0;
x The second time :a = (a ^ x) & ~b, a = (x ^ x) & ~0,a = 0; b = (b ^ x) & ~a Simplification , b = (0 ^ x) & ~0 ,b = x;
x A third time :a = (a ^ x) & ~b, a = (0 ^ x) & ~x ,a = 0; b = (b ^ x) & ~a Simplification , b = (x ^ x) & ~0, b = 0
So three times the same number ,a and b It's all back to 0; Only once , According to the above x The law of the first time is known a = x,b = 0; So finally return to a
class Solution {
public:
int singleNumber(vector<int>& nums) {
int a = 0;
int b = 0;
for(int num : nums){
a = (a ^ num) & ~b;
b = (b ^ num) & ~a;
}
return a;
}
};
2. Method 2 : Count the bits in all binary representations

Create a length of 32 Array of countscounts , Through the above method, the of each binary bit of all numbers can be recorded 11 Number of occurrences of
int count[32] = {
0};
for(int num : nums){
for(int i = 31; i >= 0; i--){
// Index small subscript , Store in high position ; Index large subscript , Store in low position
count[i] += (num & 1);
num >>= 1;
}
}
take countscounts Each element pair 33 Seeking remainder , The result is “ A number that appears only once ” Each binary bit of
utilize Move left operation and Or operations , Can be counts The value of each binary in the array is restored to the number res On
for(int i = 0; i < 32; i++){
// When you recover , To recover from the high position , That is, start with a small subscript
ans <<= 1;
ans |= (count[i] % mod);
}

Use an array to count the bits in all binary representations , How many times 1 Write it all down , Finally, use the statistical data of this array to recover
for example :3,4,3,3
3 : 11
4 : 100
3 : 11
3 : 11
Array count Element is :[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,3,3]
On final recovery , Recover from high to low , Yes count Elements of array mod 3, Then restore
class Solution {
public:
int singleNumber(vector<int>& nums) {
int count[32] = {
0};
for(int num : nums){
for(int i = 31; i >= 0; i--){
// Index small subscript , Store in high position ; Index large subscript , Store in low position
count[i] += (num & 1);
num >>= 1;
}
}
int ans = 0;
int mod = 3;
for(int i = 0; i < 32; i++){
// When you recover , To recover from the high position , That is, start with a small subscript
ans <<= 1;
ans |= (count[i] % mod);
}
return ans;
}
};
actually , Just modify the remainder value m , It can be solved except for one number , The rest of the numbers appear m Time The general problem of
边栏推荐
- Graduation thesis project local deployment practice
- DelayQueue延迟队列的使用和场景
- U-Boot初始化及工作流程分析
- 【Node】npm、yarn、pnpm 区别
- Ggplot2 drawing learning notes in R
- Basic series of SHEL script (III) for while loop
- The golang timer uses the stepped pit: the timer is executed once a day
- R language learning notes 1
- [untitled]
- SD_ CMD_ RECEIVE_ SHIFT_ REGISTER
猜你喜欢

Pytorch has been installed in anaconda, and pycharm normally runs code, but vs code displays no module named 'torch‘

Using GEE plug-in in QGIS

并查集理论讲解和代码实现

Import CV2 prompt importerror: libgl so. 1: Cannot open shared object file: no such file or directory

Microservice registry Nacos introduction

CADD课程学习(6)-- 获得已有的虚拟化合物库(Drugbank、ZINC)

An article was opened to test the real situation of outsourcing companies

PHY drive commissioning - phy controller drive (II)

DelayQueue延迟队列的使用和场景

Concurrent programming - deadlock troubleshooting and handling
随机推荐
Target detection series - detailed explanation of the principle of fast r-cnn
iNFTnews | 喝茶送虚拟股票?浅析奈雪的茶“发币”
[software testing] 05 -- principles of software testing
Matlab在线性代数中的应用(四):相似矩阵及二次型
Basic operation of external interrupt (keil5)
氢氧化钠是什么?
一文揭开,测试外包公司的真实情况
【Node】npm、yarn、pnpm 区别
SD_ CMD_ SEND_ SHIFT_ REGISTER
Pytorch has been installed in anaconda, and pycharm normally runs code, but vs code displays no module named 'torch‘
Negative number storage and type conversion in programs
2022年PMP项目管理考试敏捷知识点(7)
Reading literature sorting 20220104
1290_ Implementation analysis of prvtaskistasksuspended() interface in FreeRTOS
行测--资料分析--fb--高照老师
SOC_ SD_ CMD_ FSM
Application of MATLAB in Linear Algebra (4): similar matrix and quadratic form
【无标题】
Import CV2, prompt importerror: libcblas so. 3: cannot open shared object file: No such file or directory
C learning notes