当前位置:网站首页>128. longest continuous sequence hash table
128. longest continuous sequence hash table
2022-06-12 08:57:00 【Mr Gao】
128. The longest continuous sequence
Given an array of unsorted integers nums , Find the longest sequence of consecutive numbers ( Sequence elements are not required to be contiguous in the original array ) The length of .
Please design and implement it with a time complexity of O(n) The algorithm to solve this problem .
Example 1:
Input :nums = [100,4,200,1,3,2]
Output :4
explain : The longest consecutive sequence of numbers is [1, 2, 3, 4]. Its length is 4.
Example 2:
Input :nums = [0,3,7,2,5,8,4,6,0,1]
Output :9
This question is actually problematic , Even a practical hash table cannot achieve o(1) The requirements of , There are certain problems in the title :
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++) // Traversing the array for the first time ———— Put the values of the array into the hash table ( Remove the weight at the same time )
{
struct MyNode* findNode=Find(nums[i],HashForNums);
if(findNode==NULL) Insert(nums[i],HashForNums);
}
for(int i=0;i<numsSize;i++) // Traverse the array for the second time ———— Find the length of a long continuous sequence
{
if(nums[i]==-2147483647 || Find(nums[i]-1,HashForNums)==NULL) // Only x-1 Look back when it doesn't exist ( Be careful : The former comparison condition is to ensure that the type is int Of nums[i] Do not cross the boundary after subtracting one )
{
int curMax=0,temp=0;
while(Find(nums[i]+temp,HashForNums)!=NULL)
{
curMax++;
temp++;
}
max=fmax(curMax,max);
}
}
return max;
}
边栏推荐
- Background location case II
- torch. logical_ And() method
- 【字符集九】gbk拷贝到Unicode会乱码?
- ISCSI详解(五)——ISCSI客户端配置实战
- 抓取屏幕与毛玻璃效果
- The database doesn't know what went wrong
- You have an error in your SQL syntax; use near ‘and title=‘xxx‘‘ at line 5
- Notes used by mqtt (combined with source code)
- ip、DNS、域名、URL、hosts
- 深拷贝与浅拷贝的区别
猜你喜欢

Can you migrate backwards before the first migration in the south- Can you migrate backwards to before the first migration in South?

xshell启动遇到“由于找不到mfc110.dll,无法继续执行代码的解决方法”

Unittest测试框架

Redis installation test
![[GUI development] browsing function implementation model of image processing software](/img/37/2162a6047682b9cfc9b8b7c2488068.jpg)
[GUI development] browsing function implementation model of image processing software

torch. logical_ And() method

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

2022 simulated examination platform operation of high voltage electrician work license question bank

svg中viewbox图解分析
![[computer use] how to change a computer disk into a mobile disk?](/img/ff/843f4220fcaefc00980a6edc29aebf.jpg)
[computer use] how to change a computer disk into a mobile disk?
随机推荐
[character set 9] will GBK be garbled when copied to unicode?
Construction of memcached cache service under Linux:
ISCSI详解(五)——ISCSI客户端配置实战
Random acquisition of 4-digit non repeated verification code
Convert spaces to < br / > newline labels
Shell basic syntax -- array
[data storage] storage of floating point data in memory
Wechat applet image saving function
Technology cloud report: how will the industrial Internet rebuild the security boundary in 2022?
2022.6.9-----leetcode. four hundred and ninety-seven
《MATLAB 神經網絡43個案例分析》:第7章 RBF網絡的回歸--非線性函數回歸的實現
UMI packaging and subcontracting, and compressing to gzip
Background attribute compound writing
Background fixing effect
Redis installation test
第五章-[bx]和Loop指令
Build personal blog and web.
抓取屏幕与毛玻璃效果
2022 safety officer-c certificate special operation certificate examination question bank and simulation examination
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