当前位置:网站首页>[leetcode daily question] - 268. Missing numbers
[leetcode daily question] - 268. Missing numbers
2022-07-26 15:18:00 【IronmanJay】
List of articles
One 【 Topic category 】
- Array
Two 【 questions 】
- Simple
3、 ... and 【 Title number 】
- 268. Missing numbers
Four 【 Title Description 】
- Given an inclusion [0, n] in n Array of Numbers nums , find [0, n] The number that doesn't appear in the array in this range .
5、 ... and 【 Title Example 】
Example 1:
Input :nums = [3,0,1]
Output :2
explain :n = 3, Because there is 3 A digital , So all the numbers are in range [0,3] Inside .2 It's the missing number , Because it didn't show up in nums in .Example 2:
Input :nums = [0,1]
Output :2
explain :n = 2, Because there is 2 A digital , So all the numbers are in range [0,2] Inside .2 It's the missing number , Because it didn't show up in nums in .Example 3:
Input :nums = [9,6,4,2,3,5,7,0,1]
Output :8
explain :n = 9, Because there is 9 A digital , So all the numbers are in range [0,9] Inside .8 It's the missing number , Because it didn't show up in nums in .Example 4:
Input :nums = [0]
Output :1
explain :n = 1, Because there is 1 A digital , So all the numbers are in range [0,1] Inside .1 It's the missing number , Because it didn't show up in nums in .
6、 ... and 【 Topic tips 】
- n == nums.length
- 1 < = n < = 1 0 4 1 <= n <= 10^4 1<=n<=104
- 0 <= nums[i] <= n
- nums All the numbers in are unique
7、 ... and 【 Advanced title 】
- Can you achieve linear time complexity 、 The algorithm only uses extra constant space to solve this problem ?
8、 ... and 【 Their thinking 】
- This kind of topic is easy to think of Hash The idea of the table , Because the title says that all the numbers are unique , It also tells that the maximum value will not exceed the length of the array
- So let's first create an array map, Initialize to 0
- Then scan the incoming array , A certain number has appeared , will map The value of the corresponding position is set to 1, Indicates that there has been
- Finally scan 0~n, If map The corresponding position is 1, It means that , No operation ; If map The corresponding position is 0, Description does not appear , Just return this value
- In this way, the advanced requirements of the topic are achieved : Linear time complexity 、 An algorithm that uses only the extra constant space
Nine 【 Time frequency 】
- Time complexity : O ( N ) O(N) O(N), among N N N Is array length
- Spatial complexity : O ( N ) O(N) O(N), among N N N Is array length
Ten 【 Code implementation 】
- Java Language version
package Array;
public class p268_MissingNumbers {
public static void main(String[] args) {
int[] nums = {
3, 0, 1};
int res = missingNumber(nums);
System.out.println("res = " + res);
}
public static int missingNumber(int[] nums) {
int[] map = new int[nums.length + 1];
for (int i = 0; i < nums.length + 1; i++) {
map[i] = 0;
}
for (int i = 0; i < nums.length; i++) {
map[nums[i]] = 1;
}
for (int i = 0; i < nums.length + 1; i++) {
if (map[i] == 0) {
return i;
}
}
return 0;
}
}
- C Language version
#include<stdio.h>
#include<stdlib.h>
int missingNumber(int* nums, int numsSize)
{
int* map = (int*)calloc(numsSize + 1, sizeof(int));
for (int i = 0; i < numsSize; i++)
{
map[nums[i]] = 1;
}
for (int i = 0; i <= numsSize; i++)
{
if (map[i] == 0)
{
return i;
}
}
return NULL;
}
/* The main function is omitted */
11、 ... and 【 Submit results 】
Java Language version

C Language version

边栏推荐
- Prometheus adds redis and MySQL node monitoring
- oss删除当前时间前两天的所有文件
- Which software must be used by scientific researchers to read literature?
- [leave some code] Apply transformer to target detection, and understand the model operation process of the model through debug
- R language ggplot2 visualization: use the ggballoonplot function of ggpubr package to visualize the balloon graph (visualize the contingency table composed of two classification variables), and config
- R语言ggplot2可视化:使用ggpubr包的ggdotplot函数可视化点阵图(dot plot)、设置add参数添加均值和标准差竖线、设置error.plot参数实际显示箱体
- 解决Typora图片显示不出来问题
- 晋拓股份上交所上市:市值26亿 张东家族企业色彩浓厚
- 持续集成(二)Jenkins基本使用介绍
- 基于物联网的环境调节系统(ESP32-C3+Onenet+微信小程序)
猜你喜欢

Chapter 08_ Principles of index creation and design

大学生如何申请实用新型专利?

DevSecOps,让速度和安全兼顾

Driver development environment

Huawei applications have called the checkappupdate interface. Why is there no prompt for version update in the application

数据权限就该这么设计,yyyds!

Character function and string function and memory function

2. Add two numbers

OSPF and mGRE experiments

【LeetCode每日一题】——268.丢失的数字
随机推荐
哪里有写毕业论文需要的外文文献?
Digital commerce cloud: lead the digital upgrading of chemical industry and see how Mobei can quickly open up the whole scene of mutual integration and interoperability
兆骑科创高端人才项目引进落地,双创大赛承办,线上直播路演
How to query foreign literature?
Ner of NLP: Exploration and practice of product title attribute recognition
Simulation of character function and string function
FOC learning notes - coordinate transformation and simulation verification
7. In JS [] = =! [] Why is it true?
DICOM learning materials collection
筑牢生态安全防线,广州开展突发环境事件应急演练
Within a week, I developed my own knowledge sharing platform
R language ggplot2 visualization: visual line graph, visual line graph for different groups using the group parameter in AES function
持续集成(二)Jenkins基本使用介绍
食品制造企业想要实现智能协同的供应商管理,选择SRM供应商系统就够了
双屏协作效率翻倍 灵耀X双屏Pro引领双屏科技新潮流
The practice of software R & D should start from the design
How to write the format of a university thesis?
【静态代码质量分析工具】上海道宁为您带来SonarSource/SonarQube下载、试用、教程
If food manufacturing enterprises want to realize intelligent and collaborative supplier management, it is enough to choose SRM supplier system
数据挖掘之数据预处理