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

link : The number of occurrences of numbers in an array
The question is “ Missing numbers ” An advanced version of , Readers who haven't touched can read this first :
link : Missing numbers
Ideas : We still use XOR , Only this problem needs to find two numbers , So we have to find the XOR of these two numbers first :
First, the array nums The numbers in are XOR again , The result is the XOR number of the two numbers that appear only once .
And because the question requires that returnSize Change to a number that appears only once , Here's the easy part , Just two .
int* singleNumbers(int* nums, int numsSize, int* returnSize){
*returnSize = 2;
int *arr=(int*)malloc(sizeof(int)*2);
int n = 0;
for(int i = 0; i < numsSize; i++)
{
n ^= nums[i];
}
// follow-up ...
}
Now we have n, How do you know next n What are the two numbers ?
Take example 1 in the title as an example , Now? n The value of is 7(0111):
And we found a rule , Is that if n One of them is 1, That must be between these two numbers , One of them is 1, One of them is 0, To make n This bit of is equal to 1.
So we thought of a way to find these two numbers :
stay n From right to left in the binary bits of , Find the first one as 1 Number of digits , Then write down this bit as j, And then nums All the numbers in are judged in turn , If in j Position as 1 Then put it into an array , by 0 Then put it into another array . Finally, XOR the two arrays respectively , You can work out these two numbers !
Take example 1 here as an example , Let's find out from above n be equal to 0111, So the first one is 1 It's just right. It's the first , And then put nums The first bit in the array is 1 Put it in an array , by 0 And put it in another array .
int j = 0;// Used to store the first one as 1 Number of digits
while(n > 0)
{
if(((n>>j) & 1) != 1)
{
j++;
}
else
{
break;
}
}
int arr0[10000] = {
0};
int n0 = 0;
int arr1[10000] = {
0};
int n1 = 0;
for(int i = 0; i < numsSize; i++)
{
if(((nums[i]>>j) & 1) == 0)
{
arr0[n0] = nums[i];
n0++;
}
else
{
arr1[n1] = nums[i];
n1++;
}
}
Finally, find the two numbers in the two arrays , Because these two numbers have been separated into two arrays .
int tmp0 = 0;
int tmp1 = 0;
for(int i = 0; i < n0; i++)
{
tmp0 ^= arr0[i];
}
for(int i = 0; i < n1; i++)
{
tmp1 ^= arr1[i];
}
arr[0] = tmp0;
arr[1] = tmp1;
return arr;

Pass through !
Complete code :
int* singleNumbers(int* nums, int numsSize, int* returnSize){
*returnSize = 2;
int *arr=(int*)malloc(sizeof(int)*2);
int n = 0;
for(int i = 0; i < numsSize; i++)
{
n ^= nums[i];
}
int j = 0;// Used to store the first one as 1 Number of digits
while(n > 0)
{
if(((n>>j) & 1) != 1)
{
j++;
}
else
{
break;
}
}
int arr0[10000] = {
0};
int n0 = 0;
int arr1[10000] = {
0};
int n1 = 0;
for(int i = 0; i < numsSize; i++)
{
if(((nums[i]>>j) & 1) == 0)
{
arr0[n0] = nums[i];
n0++;
}
else
{
arr1[n1] = nums[i];
n1++;
}
}
int tmp0 = 0;
int tmp1 = 0;
for(int i = 0; i < n0; i++)
{
tmp0 ^= arr0[i];
}
for(int i = 0; i < n1; i++)
{
tmp1 ^= arr1[i];
}
arr[0] = tmp0;
arr[1] = tmp1;
return arr;
}
Don't forget to praise after learning !
边栏推荐
- Online yaml to JSON tool
- 12. Détection d'objets Mask rcnn
- The secondary market is full of bad news. How should the market go next? One article will show you the general trend
- This thing is called a jump watch?
- Summary of software testing cognition
- MySQL connection query is easy to understand
- 每日一练:删除有序数组中的重复项
- Analysis of CSRF Cross Site Request Forgery vulnerability
- 请问指南针股票软件可靠吗?在上面交易股票安全吗?
- Xiaobai's e-commerce business is very important to choose the right mall system!
猜你喜欢

6.28 learning content

每日一题:移除元素

三個pwn題

stm32F407-------时钟系统(SystemInit时钟初始化、Systick滴答定时器)

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

Stm32f407 ------- GPIO input experiment

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

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

Trois questions PWN

MySQL connection query is easy to understand
随机推荐
MySQL connection query is easy to understand
Is the compass stock software reliable? Is it safe to trade stocks on it?
Notes: three ways to define setters and Getters
Common mistakes in software testing
11. target segmentation
stm32F407-------外部中断
【LeetCode】21. Merge two ordered linked lists - go language solution
[C Prime plus chapitre II Questions de programmation après la Classe]
Is it reliable and safe to avoid five in case of stock trading account opening
ES6模块
10. Yolo series
[C Primer Plus Chapter II after class programming questions]
点击劫持:X-Frame-Options未配置
[200 opencv routines] 101 adaptive median filter
stm32F407-------串行(串口)通信
【LeetCode】21. 合并两个有序链表 - Go 语言题解
Stm32f407 ------- IO pin multiplexing mapping
Résumé de Manon, 25 ans, diplômé en trois ans
随笔记:重新认识 else if
How to make two objects or arrays equal