当前位置:网站首页>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⊙)!
边栏推荐
- 11.目标分割
- Stm32f407----- capacitive touch button
- 随笔记:重新认识 else if
- HandlerThread使用及原理
- Stm32f407------- general timer
- TypeScript -- 第七节 枚举
- Notes: three ways to define setters and Getters
- 【C Primer Plus第二章课后编程题】
- Use menu resources to implement option menus and context menus
- Stm32f407 ------- IO pin multiplexing mapping
猜你喜欢

12.物体检测Mask-Rcnn

Typescript -- Section 1: basic types

With notes: insert sort --from WCC

The secondary market is full of bad news. How should the market go next? One article will show you the general trend

How to guarantee the delivery quality through the cloud effect test plan

Huawei's level 22 experts have worked hard for ten years to complete the advanced practical document of cloud native service grid. 6

TypeScript--第五节:类

LeetCode每日一题:实现strStr()

Phoenix安装教程

12. object detection mask RCNN
随机推荐
每日一题:数组中数字出现的次数2
Phoenix installation tutorial
Auto encoder
Yyds dry goods count 【 vs code work record III 】 set vs code format
What pitfalls should be avoided in the job interview for the operation post in 2022?
这玩意叫跳表?
12.物體檢測Mask-Rcnn
Typescript -- Section 2: variable declaration
Install MySQL on Windows platform (with Navicat premium 12 "using" tutorial)
ES6:let、const、箭头函数
TypeScript --第三节:接口
mysql 高可用双主同步
点击劫持:X-Frame-Options未配置
Stm32f407 ------- GPIO input experiment
Technology sharing | software development process that you must understand if you want to get started with testing
6.28 learning content
With notes: insert sort --from WCC
What will be done after digital IC Verification?
Along with the notes: methods simulating array like classes
Test experience: how testers evolve from 0 to 1