当前位置:网站首页>【LeetCode每日一题】——268.丢失的数字
【LeetCode每日一题】——268.丢失的数字
2022-07-26 14:43:00 【IronmanJay】
一【题目类别】
- 数组
二【题目难度】
- 简单
三【题目编号】
- 268.丢失的数字
四【题目描述】
- 给定一个包含 [0, n] 中 n 个数的数组 nums ,找出 [0, n] 这个范围内没有出现在数组中的那个数。
五【题目示例】
示例 1:
输入:nums = [3,0,1]
输出:2
解释:n = 3,因为有 3 个数字,所以所有的数字都在范围 [0,3] 内。2 是丢失的数字,因为它没有出现在 nums 中。示例 2:
输入:nums = [0,1]
输出:2
解释:n = 2,因为有 2 个数字,所以所有的数字都在范围 [0,2] 内。2 是丢失的数字,因为它没有出现在 nums 中。示例 3:
输入:nums = [9,6,4,2,3,5,7,0,1]
输出:8
解释:n = 9,因为有 9 个数字,所以所有的数字都在范围 [0,9] 内。8 是丢失的数字,因为它没有出现在 nums 中。示例 4:
输入:nums = [0]
输出:1
解释:n = 1,因为有 1 个数字,所以所有的数字都在范围 [0,1] 内。1 是丢失的数字,因为它没有出现在 nums 中。
六【题目提示】
- n == nums.length
- 1 < = n < = 1 0 4 1 <= n <= 10^4 1<=n<=104
- 0 <= nums[i] <= n
- nums 中的所有数字都 独一无二
七【题目进阶】
- 你能否实现线性时间复杂度、仅使用额外常数空间的算法解决此问题?
八【解题思路】
- 这种题目很容易就想到Hash表的思想,因为题目说了所有数字独一无二,还告诉了最大值不会超过数组长度
- 所以我们首先新建一个数组map,初始化为0
- 然后扫描传入的数组,某个数字出现过,就将map对应位置的值置为1,表示出现过
- 最后再扫描0~n,如果map对应位置为1,说明出现过,不进行操作;如果map对应位置为0,说明未出现过,返回这个值即可
- 这样就实现了题目进阶所要求的:线性时间复杂度、仅使用额外常数空间的算法
九【时间频度】
- 时间复杂度: O ( N ) O(N) O(N),其中 N N N为数组长度
- 空间复杂度: O ( N ) O(N) O(N),其中 N N N为数组长度
十【代码实现】
- Java语言版
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语言版
#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;
}
/*主函数省略*/
十一【提交结果】
Java语言版

C语言版

边栏推荐
- AMB | towards sustainable agriculture: rhizosphere microbial engineering
- Simple implementation of pytorch
- 基于物联网的环境调节系统(ESP32-C3+Onenet+微信小程序)
- BSN IPFS(星际文件系统)专网简介、功能、架构及特性、接入说明
- Self encoder AE (autoencoder) program
- WPF 常用功能整合
- Summary of target tracking related knowledge
- Flask send_ Absolute path traversal caused by file function
- [dry goods] data structure and algorithm principle behind MySQL index
- Difference between filter and interceptor
猜你喜欢

Simple implementation of pytorch

Transc knowledge representation model

Whaledi message queue stability improvement practice

AMB | 迈向可持续农业:根际微生物工程

Error reported by Nacos enabled client

中值滤波器

14. Bridge-Based Active Domain Adaptation for Aspect Term Extraction 阅读笔记

Minecraft 1.16.5模组开发(五十二) 修改原版生物战利品 (Loot Table)

Brief description of llcc68 broadcast wake-up

Minecraft 1.16.5 module development (52) modify the original biological trophy (lot table)
随机推荐
JS analog clock with text label
Siamrpn: recommended regional network and twin network
【文件上传漏洞-06】分布式配置文件攻击实验—以upload-labs-4为例
Selenium code storage
双屏协作效率翻倍 灵耀X双屏Pro引领双屏科技新潮流
C # use shift > > and operation and & to judge whether the two binary numbers have changed
如何查询外文文献?
2. 两数相加
selenium 代码存放
自编码器 AE(AutoEncoder)程序
[file upload vulnerability-06] distributed configuration file attack experiment - take upload-labs-4 as an example
Introduction to C language must brush the daily question of the collection of 100 questions (1-20)
华为应用已经调用了checkAppUpdate接口,为什么应用内不提示版本更新
哪里有写毕业论文需要的外文文献?
median filter
AMB | towards sustainable agriculture: rhizosphere microbial engineering
Advanced MySQL v. InnoDB data storage structure
Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community
Sqldeveloper tools quick start
JMeter distributed