当前位置:网站首页>128. Plus longue séquence continue - table de hachage
128. Plus longue séquence continue - table de hachage
2022-06-12 08:57:00 【M. Gao】
128. La plus longue séquence continue
Compte tenu d'un tableau entier non trié nums ,Trouvez la plus longue séquence continue de nombres(Il n'est pas nécessaire que les éléments de séquence soient continus dans le tableau original)Longueur.
Veuillez concevoir et mettre en œuvre la complexité temporelle O(n) Algorithme pour résoudre ce problème.
Exemple 1:
Entrée:nums = [100,4,200,1,3,2]
Produits:4
Explication:La séquence numérique continue la plus longue est [1, 2, 3, 4].Sa longueur est 4.
Exemple 2:
Entrée:nums = [0,3,7,2,5,8,4,6,0,1]
Produits:9
C'est un problème.,Même les tables de hachage utilitaires ne peuvent pas atteindreo(1)Exigences,Le problème est qu'il y a un problème:
struct MyNode{
int data;
struct MyNode* next;
};
struct HashTbl{
int TableSize;
struct MyNode** TheLists;
};
int NextPrime(int n)
{
if(n%2==0) n++;
for(;;n+=2)
{
for(int i=3;i<=sqrt(n);i++)
if(n%i==0) goto out;
return n;
out:;
}
}
int Hash(int key,int TableSize)
{
int ret=abs(key)%TableSize;
return ret;
}
struct HashTbl* InitializeHashTbl(int TableSize)
{
struct HashTbl* H=(struct HashTbl*)malloc(sizeof(struct HashTbl));
H->TableSize=NextPrime(TableSize);
H->TheLists=malloc(sizeof(struct MyNode*)*H->TableSize);
for(int i=0;i<H->TableSize;i++)
{
H->TheLists[i]=malloc(sizeof(struct MyNode));
H->TheLists[i]->next=NULL;
H->TheLists[i]->data=-1;
}
return H;
}
struct MyNode* Find(int key,struct HashTbl* H)
{
struct MyNode* p,*l;
l=H->TheLists[Hash(key,H->TableSize)];
p=l->next;
while(p!=NULL&&p->data!=key)
p=p->next;
return p;
}
void Insert(int key,struct HashTbl* H)
{
struct MyNode* Pos,*l;
Pos=Find(key,H);
if(Pos==NULL)
{
struct MyNode* NewCell=(struct MyNode*)malloc(sizeof(struct MyNode));
l=H->TheLists[Hash(key,H->TableSize)];
NewCell->next=l->next;
l->next=NewCell;
NewCell->data=key;
}
}
void FreeHashTbl(struct HashTbl* H)
{
struct MyNode* temp,*deleteNode;
for(int i=0;i<H->TableSize;i++)
{
temp=H->TheLists[i];
while(temp!=NULL)
{
deleteNode=temp;
temp=temp->next;
free(deleteNode);
}
}
free(H);
}
int longestConsecutive(int* nums, int numsSize){
int max=0;
struct HashTbl* HashForNums=InitializeHashTbl(numsSize);
for(int i=0;i<numsSize;i++) //Première traversée du tableau———— Mettre la valeur du tableau dans la table de hachage (Et en même temps, on se débarrasse du poids)
{
struct MyNode* findNode=Find(nums[i],HashForNums);
if(findNode==NULL) Insert(nums[i],HashForNums);
}
for(int i=0;i<numsSize;i++) // Deuxième traversée du tableau ———— Trouver une longue longueur de séquence continue
{
if(nums[i]==-2147483647 || Find(nums[i]-1,HashForNums)==NULL) //Seulementx-1 Regardez en arrière quand il n'existe pas (Attention!: La condition de comparaison précédente est de s'assurer que le type est intDenums[i] Ne pas franchir la ligne après moins un )
{
int curMax=0,temp=0;
while(Find(nums[i]+temp,HashForNums)!=NULL)
{
curMax++;
temp++;
}
max=fmax(curMax,max);
}
}
return max;
}
边栏推荐
- Method to limit the input box to only numbers
- The difference between deep copy and shallow copy
- 第五章-[bx]和Loop指令
- 2022 low voltage electrician retraining question bank and online simulation examination
- (十三)文本渲染Text
- Analysis of 43 cases of MATLAB neural network: Chapter 7 regression of RBF Network -- Realization of nonlinear function regression
- Regular verification user name
- Regularization to limit the number of digits after the decimal point of an input number
- Composition of box model
- 【数据存储】浮点型数据在内存中的存储
猜你喜欢

MFS详解(四)——MFS管理服务器安装与配置

The difference between deep copy and shallow copy

43 cas d'analyse du réseau neuronal MATLAB: chapitre 7 régression du réseau RBF - - réalisation de la régression fonctionnelle non linéaire

【字符集九】gbk拷贝到Unicode会乱码?

EIP-1559

torch. logical_ And() method

Unittest测试框架

Background position - exact units

svg中viewbox图解分析

Flink CheckPoint : Exceeded checkpoint tolerable failure threshold
随机推荐
Audio and video engineer (Preliminary) (I) basic concepts of audio and video
(十四)InputField逻辑分析
深拷贝与浅拷贝的区别
Jump to an interface at a specified time. (Web Development)
Application method of new version UI of idea + use method of non test qualification and related introduction
第五章-[bx]和Loop指令
When the uniapp page jumps with complex data parameters.
Background location case 1
机器学习笔记 - 循环神经网络备忘清单
The database doesn't know what went wrong
Display the remaining valid days according to the valid period
(node:22344) [DEP0123] DeprecationWarning: Setting the TLS ServerName to an IP address is not permit
Flink CheckPoint : Exceeded checkpoint tolerable failure threshold
You have an error in your SQL syntax; use near ‘and title=‘xxx‘‘ at line 5
[sklearn] lightgbm
Can you migrate backwards before the first migration in the south- Can you migrate backwards to before the first migration in South?
Building a cluster: and replacing with error
Use NVM to dynamically adjust the nodejs version to solve the problem that the project cannot be run and packaged because the node version is too high or too low
2022 simulated examination platform operation of high voltage electrician work license question bank
Does database and table splitting cause reading diffusion problems? How to solve it?