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

link : Missing numbers
Usage sort
Because the title says that this array is from 0 To n All integers of , So the first thing you think of may be sorting this array, such as bubble sorting .
Although the idea is good , But the question asks whether it can be done in O(n) Time complexity complete , So here Sorting is not recommended .
Using summation
Because of a feature of this array : It's from 0 To n All integers of , Only one is missing .
Then I have an idea , It's about putting 0 To n Add them all together , Subtract the sum of this array , It is equal to the missing number !, Simply put, it's like this :
0 To n The sum of an arithmetic sequence - The sum of all numbers in the array = Missing numbers
int missingNumber(int* nums, int numsSize){
// The sum of an arithmetic sequence
int all = ((0 + numsSize) * (numsSize + 1)) / 2;
// The sum of array elements
int sum = 0;
for(int i = 0; i < numsSize; i++)
{
sum += nums[i];
}
return all - sum;
}
Using XOR
First of all, we should know that XOR has a characteristic , You take a number to XOR a number , Take the XOR number to XOR one of the original numbers , The result is another XOR number .
Based on this feature , We have this idea :
use 0 Remove every number in the XOR array , And then XOR 0 To n Every number , The final result is the number of disappearances .
int missingNumber(int* nums, int numsSize){
int n=0;
for(int i=0;i<numsSize;i++)
n^=nums[i];
for(int i=0;i<=numsSize;i++)// Here is n+1 A digital , Remember to add the equals sign
n^=i;
return n;
}
Learn knowledge of the friends do not mean your praise (⊙o⊙)!
边栏推荐
- LinkedIn DataHub --- 经验分享
- How to solve the database type error in the operation of the servert project?
- JVM工作原理介绍
- 12.物体检测Mask-Rcnn
- 10. Yolo series
- stm32F407-------RTC实时时钟
- The second session of question swiping and clock out activity -- solving the switching problem with recursion as the background (2)
- PHP函数file_get_contents与操作系统的内存映射
- mysql 高可用双主同步
- 这玩意叫跳表?
猜你喜欢

矩 阵 压 缩

MSYQL is abnormal. Don't worry. Mr. Allen will point out the puzzle

6.28 学习内容

How many locks are added to an update statement? Take you to understand the underlying principles

Analysis of CSRF Cross Site Request Forgery vulnerability
![Edge extraction based on Halcon learning [2] circles Hdev routine](/img/e4/e3738d71c2ff5a239a12f67d06e2c9.jpg)
Edge extraction based on Halcon learning [2] circles Hdev routine

"Five considerations" for safe use of the Internet

Common mistakes in software testing

Phoenix安装教程

Auto encoder
随机推荐
Résumé de Manon, 25 ans, diplômé en trois ans
Typescript -- Section 2: variable declaration
stm32F407-------电容触摸按键
Notes: three ways to define setters and Getters
stm32F407-------串行(串口)通信
每日一题:消失的数字
Is it reliable and safe to avoid five in case of stock trading account opening
Phoenix installation tutorial
Typescript -- Section 1: basic types
12. Détection d'objets Mask rcnn
Give you a project, how will you carry out performance testing (I)
Stm32f407 ------ running lamp and buzzer
10、YOLO系列
[machine learning] numerical analysis 02 -- finding roots of arbitrary equations
Easy to use free ppt template
Software testing tools: complete and precise
随笔记:插入排序 --from wcc
FATAL ERROR: Could not find ./bin/my_print_defaults的解决办法
[C Prime plus chapitre II Questions de programmation après la Classe]
JVM工作原理介绍