当前位置:网站首页>Vanishing numbers
Vanishing numbers
2022-07-04 09:47:00 【skeet follower】
Array nums Contains from 0 To n All integers of , But one of them is missing . Please write code to find the missing integer .
The required time complexity is O(n), The space complexity is O(1).
Example 1:
Input :[3,0,1] Output :2
Example 2:
Input :[9,6,4,2,3,5,7,0,1] Output :8
Train of thought :
Using the properties of XOR ,res = res ^ x ^ x. XOR the same value twice , Then the result is equal to itself , So we are right res from 0-nums.length To engage in exclusive or , At the same time nums XOR the values in the array , If there is a repetition, it will disappear , So finally res The value of is a number that appears only once , That is to say nums The missing number in the array .
int missingNumber(int* nums, int numsSize){
int ret=0;
int i=0;
for(i=0;i<=numsSize;i++){
ret^=i;
}
for(i=0;i<numsSize;i++)
{
ret^= nums[i];
}
return ret;
}int missingNumber(int* nums, int numsSize){
int ret=0;
int i=0;
for(i=1;i<=numsSize;i++){
ret^=i;
ret^=nums[i-1];
}
return ret;
}int missingNumber(int* nums, int numsSize){
int ret=0;
int i=0;
for(i=0;i<numsSize;i++){
ret^=i;
ret^=nums[i];
}
ret^=numsSize;
return ret;
}Train of thought two :
Before finding out n+1 Sum of numbers , Subtract the number in the array , It's the disappearing number .
int missingNumber(int* nums, int numsSize){
int i=0;
int sum=0;
for(i=0;i<numsSize+1;i++){
sum+=i;
}
for(i=0;i<numsSize;i++){
sum-=nums[i];
}
return sum;
}Train of thought three :
Open up another large space array , The number of the original array is used as the index of the new array , Then mark it as 1, Then traverse the new array instead of 1 The index of is what you want .
int missingNumber(int* nums, int numsSize){
int i;
int* tmp=(int* )malloc(sizeof(int)*(numsSize+1));
for(i=0;i<numsSize;i++){
tmp[nums[i]]=1;
}
for(i=0;i<numsSize;i++){
if(tmp[i]!=1)
break;
}
return i;
}Train of thought four :
Hashtable
int missingNumber(int* nums, int numsSize){
int f[10000]={0};
int i;
for(i=0;i<numsSize;i++){
f[nums[i]]++;
}
for(i=0;i<10000;i++){
if(f[i]==0)
break;
}
return i;
}Is there any good way , Leave a message in the comment area and we will discuss .
边栏推荐
- Deadlock in channel
- Golang type comparison
- Launpad | 基礎知識
- 查看CSDN个人资源下载明细
- PHP personal album management system source code, realizes album classification and album grouping, as well as album image management. The database adopts Mysql to realize the login and registration f
- Write a jison parser from scratch (4/10): detailed explanation of the syntax format of the jison parser generator
- Global and Chinese markets of hemoglobin analyzers in care points 2022-2028: Research Report on technology, participants, trends, market size and share
- Multilingual Wikipedia website source code development part II
- Flutter 小技巧之 ListView 和 PageView 的各種花式嵌套
- Lauchpad x | MODE
猜你喜欢

SQL replying to comments

Hands on deep learning (41) -- Deep recurrent neural network (deep RNN)

PHP is used to add, modify and delete movie information, which is divided into foreground management and background management. Foreground users can browse information and post messages, and backgroun

Nuxt reports an error: render function or template not defined in component: anonymous

Qtreeview+ custom model implementation example

MATLAB小技巧(25)竞争神经网络与SOM神经网络

libmysqlclient.so.20: cannot open shared object file: No such file or directory

2022-2028 global tensile strain sensor industry research and trend analysis report

Hands on deep learning (43) -- machine translation and its data construction

Hands on deep learning (44) -- seq2seq principle and Implementation
随机推荐
2022-2028 global seeder industry research and trend analysis report
2022-2028 global visual quality analyzer industry research and trend analysis report
How to display √ 2 on the command line terminal ̅? This is actually a blog's Unicode test article
xxl-job惊艳的设计,怎能叫人不爱
2022-2028 global optical transparency industry research and trend analysis report
How do microservices aggregate API documents? This wave of show~
What are the advantages of automation?
Write a mobile date selector component by yourself
Four common methods of copying object attributes (summarize the highest efficiency)
Daughter love: frequency spectrum analysis of a piece of music
【leetcode】29. Divide two numbers
Investment analysis and future production and marketing demand forecast report of China's paper industry Ⓥ 2022 ~ 2028
El Table Radio select and hide the select all box
C语言指针经典面试题——第一弹
Trees and graphs (traversal)
Fabric of kubernetes CNI plug-in
2022-2028 global elastic strain sensor industry research and trend analysis report
Hands on deep learning (45) -- bundle search
C language pointer interview question - the second bullet
libmysqlclient.so.20: cannot open shared object file: No such file or directory