当前位置:网站首页>[interview question 17.04. missing numbers]
[interview question 17.04. missing numbers]
2022-06-11 02:36:00 【Cat star people who love Durian】
List of articles
One 、 Subject requirements
Array nums Contains from 0 To n All integers of , But one of them is missing . Please write code to find the missing integer . You have a way O(n) Is it finished in time ?
Example 1:
Input :[3,0,1]
Output :2
Example 2:
Input :[9,6,4,2,3,5,7,0,1]
Output :8
source : Power button (LeetCode)
link :https://leetcode.cn/problems/missing-number-lcci/
Two 、 Thought analysis
Train of thought :
n Add the numbers and subtract nums Add all numbers
int missingNumber(int* nums, int numsSize){
int sum = (0 + numsSize) * (numsSize + 1) / 2;
int sumnum = 0;
for(int i = 0;i < numsSize; i++)
{
sumnum += nums[i];
}
return sum - sumnum;
}
Train of thought two :
n Number XOR , And again nums Exclusive or
Tips :0 And any number XOR gets itself ( Self means anything that is related to 0 The number of exclusive or )
int missingNumber(int* nums, int numsSize){
int sum = 0;
for(int i = 0 ;i <= numsSize ;i++)
{
sum ^= i;
}
for(int i = 0 ;i < numsSize ;i++)
{
sum ^= nums[i];
}
return sum;
}
The above is the whole content of this article , If there are mistakes in the article or something you don't understand , Communicate more with meow bloggers . Learn from each other and make progress . If this article helps you , Can give meow bloggers a concern , Your support is my biggest motivation .
边栏推荐
- Jetpack compose scaffold and topappbar (top navigation)
- Bingbing learning notes: find the greatest common divisor and the least common multiple. Complex version reverse string
- 金属有机骨架材料Fe-MIL-53,Mg-MOF-74,Ti-KUMOF-1,Fe-MIL-100,Fe-MIL-101)负载异氟醚/甲氨蝶呤/阿霉素(DOX)/紫杉醇/布洛芬/喜树碱
- PHP starts OpenSSL and reports OpenSSL support=> disabled (install ext/openssl)
- Unity determines whether the object is in the camera field of view
- Online courses avaiable
- Unity animator rewind
- Why can some programmers get good offers with average ability?
- Stc8a8k64d4 EEPROM read / write failure
- Closing method of SQL injection
猜你喜欢

Navicat Premium 15 工具自动被杀毒防护软件删除解决方法

【并行与分布式系统】Cache学习

Use of CIN and cout

Jetpack compose scaffold and topappbar (top navigation)

【189. 轮转数组】

Bingbing learning notes: find the greatest common divisor and the least common multiple. Complex version reverse string

A collection of common ADB commands for app testing

Everything实现快速搜索的原理

Li Kou brushing questions - hash table

SD3.0笔记
随机推荐
年金保險理財產品可以複利嗎?利率是多少?
Large screen - full screen, exit full screen
SQL | 外连接
MySQL备份与恢复
Databinding escaping with presentation symbols
Technology sharing | quick intercom, global intercom
Kotlin apply方法
1031. maximum sum of two non overlapping subarrays
2022 simulated 100 questions and answers for crane driver (limited to bridge crane) examination
NFT insider 61:animoca brands holds US $1.5 billion of encrypted assets in 340 investments
PHP starts OpenSSL and reports OpenSSL support=> disabled (install ext/openssl)
[untitled]
Common vocabulary of software testing English
2022 safety officer-b certificate examination question bank and answers
Sword finger offer II 079 All subsets
金属有机框架MOF-Al(DIBA),MOF-Zr(DIBA),MOF-Fe(DIBA)包载姜黄素/羧苄西林/MTX甲氨蝶呤/紫杉醇PTX/阿霉素DOX/顺铂CDDP/CPT喜树碱等药物
深度学习基础篇【4】从0开始搭建EasyOCR并进行简单文字识别
Metal organic framework materials (fe-mil-53, mg-mof-74, ti-kumof-1, fe-mil-100, fe-mil-101) supported on isoflurane / methotrexate / doxorubicin (DOX) / paclitaxel / ibuprofen / camptothecin
学习太极创客 — ESP8226 (二)
Explanation of spark common parameters