当前位置:网站首页>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 .
边栏推荐
- Golang Modules
- Global and Chinese markets of water heaters in Saudi Arabia 2022-2028: Research Report on technology, participants, trends, market size and share
- Pcl:: fromrosmsg alarm failed to find match for field 'intensity'
- Ultimate bug finding method - two points
- MySQL foundation 02 - installing MySQL in non docker version
- View CSDN personal resource download details
- 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
- Launpad | Basics
- Write a mobile date selector component by yourself
- Hands on deep learning (38) -- realize RNN from scratch
猜你喜欢

MySQL develops small mall management system

Hands on deep learning (33) -- style transfer

How can people not love the amazing design of XXL job

2022-2028 global protein confectionery industry research and trend analysis report

Matlab tips (25) competitive neural network and SOM neural network

华为联机对战如何提升玩家匹配成功几率

C # use gdi+ to add text to the picture and make the text adaptive to the rectangular area

2022-2028 research and trend analysis report on the global edible essence industry

PHP book borrowing management system, with complete functions, supports user foreground management and background management, and supports the latest version of PHP 7 x. Database mysql

2022-2028 global seeder industry research and trend analysis report
随机推荐
【leetcode】540. A single element in an ordered array
C语言指针面试题——第二弹
[on February 11, 2022, the latest and most fully available script library collection of the whole network, a total of 23]
【leetcode】29. Divide two numbers
直方图均衡化
C # use smtpclient The sendasync method fails to send mail, and always returns canceled
Write a jison parser from scratch (2/10): learn the correct posture of the parser generator parser generator
2022-2028 global seeder industry research and trend analysis report
7-17 crawling worms (15 points)
Normal vector point cloud rotation
Go context basic introduction
Global and Chinese market of wheel hubs 2022-2028: Research Report on technology, participants, trends, market size and share
Hands on deep learning (35) -- text preprocessing (NLP)
Log cannot be recorded after log4net is deployed to the server
Mmclassification annotation file generation
2022-2028 global gasket plate heat exchanger industry research and trend analysis report
libmysqlclient.so.20: cannot open shared object file: No such file or directory
Solution to null JSON after serialization in golang
法向量点云旋转
SSM online examination system source code, database using mysql, online examination system, fully functional, randomly generated question bank, supporting a variety of question types, students, teache