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

边栏推荐
- stm32F407-------LCD
- WPF implementation calls local camera~
- Trois questions PWN
- The magical zero knowledge proof can not only keep secrets, but also make others believe you!
- The company has a new Post-00 test paper king. The old oilman said that he could not do it. He has been
- Stm32f407 ------- RTC real time clock
- 在线买股票开户安全嘛?
- Windows平台下安装MySQL(附:Navicat Premium 12 “使用” 教程)
- Phoenix安装教程
- Mysql的四种引擎介绍
猜你喜欢
随机推荐
TypeScript --第三节:接口
TypeScript--第五节:类
ES6 module
How to solve the database type error in the operation of the servert project?
Technology sharing | software development process that you must understand if you want to get started with testing
mysql 高可用双主同步
Stm32f407------- general timer
[machine learning] numerical analysis 02 -- finding roots of arbitrary equations
stm32F407-------寄存器地址名称映射分析
Differences among VaR, let and Const
With notes: re understanding else if
Windows平台下安装MySQL(附:Navicat Premium 12 “使用” 教程)
Zoom with mouse wheel
stm32F407-------跑马灯、蜂鸣器
websocket-js连接如何携带token验证
What will be done after digital IC Verification?
随笔记:重新认识 else if
[buuctf.reverse] 131-135
MySQL connection query is easy to understand
小白创业做电商,选对商城系统很重要!



![SQL note 2 [MySQL]](/img/a4/f711173ce731d95860746e309b7d74.jpg)





