当前位置:网站首页>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;
}
边栏推荐
- Jump to an interface at a specified time. (Web Development)
- Flink CheckPoint : Exceeded checkpoint tolerable failure threshold
- Audio and video engineer (Preliminary) (I) basic concepts of audio and video
- Building a cluster: and replacing with error
- Analysis of 43 cases of MATLAB neural network: Chapter 7 regression of RBF Network -- Realization of nonlinear function regression
- Shell basic syntax -- arithmetic operation
- Box model border
- 2022.6.11-----leetcode. nine hundred and twenty-six
- [new planning]
- 2022.6.11-----leetcode.926
猜你喜欢
![[character set 7] what are the wide character codes and multi byte codes of Chinese characters](/img/8c/6d375d90234e6094b6930c2cefefa1.png)
[character set 7] what are the wide character codes and multi byte codes of Chinese characters

ERROR 1630 (42000): FUNCTION a.avg does not exist. Check the ‘Function Name Parsing and Resolution‘

Background attribute compound writing

【字符集八】char8_t、char16_t、char32_t、wchar、char

Build personal blog and web.

MFS explanation (IV) -- MFS management server installation and configuration

Composition of box model

Loading circling effect during loading
![[open source project] easycmd command graphical software](/img/7b/4b4d7059a5dd520f3491cc376f41fc.jpg)
[open source project] easycmd command graphical software

IDEA新版UI申请方法+无测试资格使用方法及相关介绍
随机推荐
[sklearn] lightgbm
Inheritance of row height
抓取屏幕与毛玻璃效果
js实现页面加载后再刷新一次
根据有效期显示距离当前还剩多少天有效期
Dynamically create and submit forms
ISCSI详解(五)——ISCSI客户端配置实战
Oracle installation details (verification)
【字符集八】char8_t、char16_t、char32_t、wchar、char
Shell基本语法--算数运算
Chapter VI - procedures with multiple segments
(十三)文本渲染Text
QT realizes multi screen and multi-resolution adaptation
Bash tutorial
Chapter 3 registers (memory access)
API处理Android安全距离
在Tensorflow中把Tensor转换为ndarray时,循环中不断调用run或者eval函数,代码运行越来越慢!
[untitled] task3 multiple recall
Chapter IV - first procedure
Engineers learn music theory (III) interval mode and chord