当前位置:网站首页>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 .
边栏推荐
- Summary of reasons for web side automation test failure
- Machine learning -- neural network (IV): BP neural network
- Summary of small program performance optimization practice
- 品牌连锁店5G/4G无线组网方案
- 直方图均衡化
- Hands on deep learning (35) -- text preprocessing (NLP)
- Golang defer
- 智慧路灯杆水库区安全监测应用
- Hands on deep learning (43) -- machine translation and its data construction
- 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
猜你喜欢
Hands on deep learning (36) -- language model and data set
智能网关助力提高工业数据采集和利用
2022-2028 global edible probiotic raw material industry research and trend analysis report
C language pointer classic interview question - the first bullet
How to batch change file extensions in win10
C # use gdi+ to add text to the picture and make the text adaptive to the rectangular area
ASP. Net to access directory files outside the project website
2022-2028 global gasket plate heat exchanger industry research and trend analysis report
How web pages interact with applets
2022-2028 global industrial gasket plate heat exchanger industry research and trend analysis report
随机推荐
Global and Chinese market of bipolar generators 2022-2028: Research Report on technology, participants, trends, market size and share
Kotlin: collection use
Golang Modules
Global and Chinese market of air fryer 2022-2028: Research Report on technology, participants, trends, market size and share
Hands on deep learning (35) -- text preprocessing (NLP)
智慧路灯杆水库区安全监测应用
Write a jison parser (7/10) from scratch: the iterative development process of the parser generator 'parser generator'
Machine learning -- neural network (IV): BP neural network
Hands on deep learning (39) -- gating cycle unit Gru
`Example of mask ` tool use
Deadlock in channel
Hands on deep learning (43) -- machine translation and its data construction
2022-2028 global visual quality analyzer industry research and trend analysis report
Global and Chinese market of planar waveguide optical splitter 2022-2028: Research Report on technology, participants, trends, market size and share
MySQL foundation 02 - installing MySQL in non docker version
Golang defer
2022-2028 global gasket plate heat exchanger industry research and trend analysis report
How can Huawei online match improve the success rate of player matching
Hands on deep learning (32) -- fully connected convolutional neural network FCN
2022-2028 global tensile strain sensor industry research and trend analysis report