当前位置:网站首页>Daily question 1: the number of numbers in the array 2
Daily question 1: the number of numbers in the array 2
2022-06-29 00:10:00 【Sharp blade CC】

link : The number of occurrences of numbers in an array 2
This question is another version of the previous blog , The link to the previous one is below :
link : The number of occurrences of numbers in an array 1
The difference between this question and the previous one is that the number of times it appears here is 3 And then 1 Time of , So the XOR method is not very good , We can find another way .
We want to , Since only one number appears once in this array , The others are three times , Then use an array to make these numbers appear three times , Count and add each of their bits , You will find that the number of each bit in the statistical array will be 3 Multiple , If there is another number that appears once , Then he added it to a certain binary digit , Will make the number of a bit in this array into a module 3 more than 1, Then we can find out that the number is 1 Hexadecimal digits of , Finally, the number is calculated by binary operation . in general :
Count all the numbers in the array , From 1 Position to the first 32 How many decimal digits are there 1, Then find the module in the array 3 more than 1 Number of digits , The binary bit of this once occurring number is 1 Number of digits .
int singleNumber(int* nums, int numsSize){
// Statistics first , So let's start with an array
int arr[32] = {
0};
for(int i = 0; i < numsSize; i++)
{
for(int j = 0; j < 32; j++)
{
if(((nums[i]>>j) & 1) == 1)
{
arr[j] += 1;
}
}
}
// Let's see who's here once
int n = 0;
for(int i = 0; i < 32; i++)
{
if((arr[i] % 3) == 1)
{
// Judge whether it is the No 0 Position as 1 The situation of
if(i == 0)
{
n += 1;
}
else
{
n += pow(2, i);
}
}
}
return n;
}

边栏推荐
猜你喜欢

The second session of question swiping and clock out activity -- solving the switching problem with recursion as the background (III)

Stm32f407 ------ clock system (systeminit clock initialization, systick tick timer)

stm32F407-------寄存器地址名称映射分析

What are the virtual machine software? What are their respective roles?

Phoenix installation tutorial

Xiaobai's e-commerce business is very important to choose the right mall system!

每日一题:数组中数字出现的次数2

单机多实例MYSQL主从复制

Have you ever met a fake interview in a job interview? How to avoid?

oracle 去掉html标签
随机推荐
Haskell 配置 VS code 开发环境 (2022年6月)
stm32F407-------LCD
矩 阵 压 缩
Picture 64base transcoding and decoding
每日一题:移除元素
Edge extraction based on Halcon learning [2] circles Hdev routine
MySQL connection query is easy to understand
Notes: three ways to define setters and Getters
Stm32f407 ------ serial (serial port) communication
scp拷贝文件夹
11.目标分割
LinkedIn DataHub --- 经验分享
Typescript -- Section 7 enumeration
oracle 去掉html标签
Along with the notes: methods simulating array like classes
随笔记:重新认识 else if
Stm32f407 ------ running lamp and buzzer
Stm32f407 ------ clock system (systeminit clock initialization, systick tick timer)
What will be done after digital IC Verification?
[C Primer Plus Chapter II after class programming questions]