当前位置:网站首页>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
边栏推荐
- [vscode] prohibit the pylance plug-in from automatically adding import
- Idea shortcut key
- Application of MATLAB in Linear Algebra (4): similar matrix and quadratic form
- The difference between NPM install -g/-save/-save-dev
- Daily Practice:Codeforces Round #794 (Div. 2)(A~D)
- U-boot initialization and workflow analysis
- Simple operation with independent keys (hey, a little fancy) (keil5)
- [untitled]
- Mipi interface, DVP interface and CSI interface of camera
- Now there are HTML files and MVC made with vs (connected to the database). How can they be connected?
猜你喜欢
Machine learning Seaborn visualization
[vscode] prohibit the pylance plug-in from automatically adding import
DataGrid offline installation of database driver
C#学习笔记
(tool use) how to make the system automatically match and associate to database fields by importing MySQL from idea and writing SQL statements
借助 Navicat for MySQL 软件 把 不同或者相同数据库链接中的某数据库表数据 复制到 另一个数据库表中
The mutual realization of C L stack and queue in I
网易To B,柔外刚中
Tshydro tool
第 2 章:小试牛刀,实现一个简单的Bean容器
随机推荐
Unconventional ending disconnected from the target VM, address: '127.0.0.1:62635', transport: 'socket‘
Install deeptools in CONDA mode
GPIO port bit based on Cortex-M3 and M4 with operation macro definition (can be used for bus input and output, STM32, aducm4050, etc.)
Oracle code use
I can't stand the common annotations of idea anymore
IPage能正常显示数据,但是total一直等于0
Intelligent target detection 59 -- detailed explanation of pytoch focal loss and its implementation in yolov4
【Node】nvm 版本管理工具
2022.06.27_每日一题
Unity UGUI不同的UI面板或者UI之间如何进行坐标匹配和变换
Ggplot2 drawing learning notes in R
Brief description of inux camera (Mipi interface)
The problem of configuring opencv in qt5.13.2 is solved in detail
[untitled]
[vscode] search using regular expressions
arcpy. SpatialJoin_ Analysis spatial connection analysis
Simple operation with independent keys (hey, a little fancy) (keil5)
Import CV2, prompt importerror: libcblas so. 3: cannot open shared object file: No such file or directory
第 2 章:小试牛刀,实现一个简单的Bean容器
行测--资料分析--fb--高照老师