当前位置:网站首页>128. 最長連續序列-哈希錶
128. 最長連續序列-哈希錶
2022-06-12 08:57:00 【Mr Gao】
128. 最長連續序列
給定一個未排序的整數數組 nums ,找出數字連續的最長序列(不要求序列元素在原數組中連續)的長度。
請你設計並實現時間複雜度為 O(n) 的算法解决此問題。
示例 1:
輸入:nums = [100,4,200,1,3,2]
輸出:4
解釋:最長數字連續序列是 [1, 2, 3, 4]。它的長度為 4。
示例 2:
輸入:nums = [0,3,7,2,5,8,4,6,0,1]
輸出:9
這題其實是有問題的,即使實用哈希錶也不能達到o(1)的要求,題目是存在一定問題的:
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++) //第一次遍曆數組————把數組的值放到哈希錶中(同時去重)
{
struct MyNode* findNode=Find(nums[i],HashForNums);
if(findNode==NULL) Insert(nums[i],HashForNums);
}
for(int i=0;i<numsSize;i++) //第二次遍曆數組————找長連續序列長度
{
if(nums[i]==-2147483647 || Find(nums[i]-1,HashForNums)==NULL) //只有x-1不存在的時候再往後找(注意:前一項比較條件是確保類型為int的nums[i]在减一後不越界)
{
int curMax=0,temp=0;
while(Find(nums[i]+temp,HashForNums)!=NULL)
{
curMax++;
temp++;
}
max=fmax(curMax,max);
}
}
return max;
}
边栏推荐
- 《MATLAB 神經網絡43個案例分析》:第7章 RBF網絡的回歸--非線性函數回歸的實現
- [sklearn] lightgbm
- IP, DNS, domain name, URL, hosts
- 了结非对称密钥
- [data storage] storage of floating point data in memory
- 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
- Engineers learn music theory (II) scale and tendency
- Build personal blog and web.
- 2022 simulated examination platform operation of high voltage electrician work license question bank
- day5-x
猜你喜欢

node示例后台搭建

Priority issues

Background position position NOUN

深拷贝与浅拷贝的区别

Flink传入自定义的参数或配置文件

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

Loading font component loading effect
![[GUI development] browsing function implementation model of image processing software](/img/37/2162a6047682b9cfc9b8b7c2488068.jpg)
[GUI development] browsing function implementation model of image processing software

Popular understanding of time domain sampling and frequency domain continuation
![[character set 9] will GBK be garbled when copied to unicode?](/img/dc/c9ec4a90355d30479f23fdead4b349.png)
[character set 9] will GBK be garbled when copied to unicode?
随机推荐
(node:22344) [DEP0123] DeprecationWarning: Setting the TLS ServerName to an IP address is not permit
Close asymmetric key
torch. logical_ And() method
Difference between binary GB and gib
Random acquisition of 4-digit non repeated verification code
进制GB和GiB的区别
[advanced pointer I] character array & array pointer & pointer array
day5-x
Engineers learn music theory (I) try to understand music
Construction of memcached cache service under Linux:
Convert spaces to < br / > newline labels
Union selector
Handling abnormal data
EIP-1559
Audio and video related links
Shell基本语法--算数运算
Chapter 3 registers (memory access)
svg中viewbox图解分析
[sklearn] lightgbm
Problems that cannot be resolved by tar command