当前位置:网站首页>The first positive number missing in question 41 of C language. Two methods, preprocessing, fast sorting and in situ hashing
The first positive number missing in question 41 of C language. Two methods, preprocessing, fast sorting and in situ hashing
2022-07-26 05:12:00 【Take care of two dogs and never let them go bad】
Here's an unsorted array of integers nums , Please find the smallest positive integer that doesn't appear in it .
Please implement a time complexity of O(n) And solutions that use only constant level extra space .
Example 1:
Input :nums = [1, 2, 0]
Output :3
Example 2:
Input :nums = [3, 4, -1, 1]
Output :2
Example 3:
Input :nums = [7, 8, 9, 11, 12]
Output :1
Tips :
1 <= nums.length <= 5 * 105
- 231 <= nums[i] <= 231 - 1
source : Power button (LeetCode)
link :https ://leetcode.cn/problems/first-missing-positive
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Method 1 Pretreatment accelerates the discharge
Set the array size to N, The smallest known positive integer is 1, Then let the minimum positive integer missing be n=1, Then we just need to start from 1 Start , Judge n Whether in the array , If in the array , be n Add one , If it's not in the array , Then the minimum positive integer missing is still n.
But larger numbers may appear before smaller numbers , Traversal from front to back may miss some values , So you need to traverse from the beginning every time , The time complexity is O(N^2), It's too high , So we need to improve .
Let's have a quick row from small to large , The time complexity is O(NlogN), Then traverse the array from front to back according to the previous idea , The time complexity is O(N), Then the total time complexity is O(NlogN)+O(N)=O(NlogN)
int cmp(const void *a, const void *b) {
return *(int*)a - *(int*)b;
}
int firstMissingPositive(int* nums, int numsSize) {
for(int i = 0; i < numsSize; i++) {
if(nums[i] < 0) {
nums[i] = 0;
}
}
qsort(nums, numsSize, sizeof(int), cmp);
int n = 1;
for(int i = 0; i < numsSize; i++) {
if(nums[i] == n) {
n++;
}
}
return n;
}
Method 2 : In situ hash
Because the title requires us to 「 Only constant level spaces can be used 」, And the number you're looking for must be [1, N + 1] Left and right closed ( here N Is the length of the array ) In this interval . therefore , We can use the original array as a hash table . in fact , The hash table itself is actually an array ;
The number we're looking for is [1, N + 1] in , Last N + 1 We don't have to look for this element . Because in front of N When none of the elements can be found , We came back to N + 1;
that , We can take this idea : Just put 1 This number is subscripted as 0 The location of , 2 This number is subscripted as 1 The location of , Sort out the array according to this idea . Then we iterate over the array again , The first 1 The value of an encounter is not equal to the number of subscripts , It's the first positive number we're looking for .
This idea is equivalent to writing our own hash function , The rules of this hash function are particularly simple , That is, the value is i The number of is mapped to the subscript i - 1 The location of .

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int firstMissingPositive(int* nums, int numsSize)
{
int ans = 0;
int i = 0, j = 0;
for (int i = 0; i < numsSize; ++i)
{
if (nums[i] <= 0) {
nums[i] = numsSize + 1;
}
}
for (i = 0; i < numsSize; i++)
{
while (nums[i] >= 1 && nums[i] <= numsSize && nums[i] != nums[nums[i] - 1])
{
j = nums[i];
nums[i] = nums[j - 1];
nums[j - 1] = j;
}
}
for (i = 0; i < numsSize; i++)
{
if (nums[i] != i + 1)
{
ans = i + 1;
return ans;
}
}
//for (i = 0; i < numsSize; i++)
// printf("%d\n", nums[i]);
return numsSize+1;
}
int main()
{
int nums[] = { 3, 4, -1, 1 };
int numsSize = sizeof(nums) / sizeof(int);
int ans = firstMissingPositive(nums, numsSize);
printf("%d", ans);
return 0;
}边栏推荐
猜你喜欢

Redis过期删除策略和内存淘汰策略

MySQL basic learning

kubernetes install completed

推荐12个免费查找文献的学术网站,建议点赞、收藏!

Date and time function of MySQL function summary

Go-Excelize API源码阅读(六)—— DeleteSheet(sheet string)

Unity scene jump script
![[weekly translation go] how to write your first program with go](/img/77/cf77a46340a39797382fd7b60517d5.png)
[weekly translation go] how to write your first program with go

域名解析过程全分析,就着文字理解更佳

Compilation method of flood control evaluation report and flood modeling under the new guidelines
随机推荐
Redis过期删除策略和内存淘汰策略
Excel VBA:按日期汇总计算输出结果(sumif)
MySQL eight knowledge points: from getting started to deleting the database
Leetcode linked list problem - 203. remove the linked list elements (learn the linked list by one question and one article)
提高shuffle操作中的reduce并行度
[weekly translation go] how to write your first program with go
Install nccl \ mpirun \ horovod \ NVIDIA tensorflow (3090ti)
LeetCode链表问题——206.反转链表(一题一文学会链表)
Recommendation system - machine learning
MySQL基础学习
[Luogu] p1383 advanced typewriter
C语言力扣第41题之缺失的第一个正数。两种方法,预处理快排与原地哈希
Migrate the server and reconfigure the database (the database has no monitoring, and the monitoring starts with tns-12545, tns-12560, tns-00515 errors)
Shell的read 读取控制台输入、read的使用
域名解析过程全分析,就着文字理解更佳
Embedded sharing collection 20
Map making of environmental impact assessment based on remote sensing interpretation and GIS technology
Test of countlaunch demo
注解@Autowired如何自动装配
【Leetcode】493. Reverse Pairs