当前位置:网站首页>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;
}边栏推荐
猜你喜欢

【ACWing】1268. 简单题

面试之请详细说下synchronized的实现原理以及相关的锁

时代潮流-云原生数据库的崛起

When AQS wakes up the thread, I understand why it traverses from the back to the front

MySQL八股知识点:从入门到删库

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

分布式ID的常用解决方案-一把拿下

Recommend 12 academic websites for free literature search, and suggest to like and collect!

MySQL basic learning

Teach you how to use code to realize SSO single sign on
随机推荐
嵌入式分享合集20
Ansible中常用的模块
如何优雅的复现YOLOv5官方历程(二)——标注并训练自己的数据集
五个维度着手MySQL的优化,我和面试官都聊嗨了
Test of countlaunch demo
时代潮流-云原生数据库的崛起
security权限管理详解
Getting started with ALV
Security permission management details
Textfield and password input box that are more flexible and easy to use in compose
How many holes have you stepped on in BigDecimal?
Unnamed Article 33
Redis solves the problem of oversold inventory
一次线上事故,我顿悟了异步的精髓
Ansible tutorial
公交站间的距离 : 简单模拟题
Annotation @autowired how to assemble automatically
[pytorch] install torch 1.8.1 and check whether torch version and GPU are available
ALV程序收集
YOLOv5执行全过程----目录