当前位置:网站首页>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个案例分析》:第8章 GRNN网络的预测----基于广义回归神经网络的货运量预测
- Handling abnormal data
- 【字符集七】汉字的宽字符码和多字节码分别是多少
- ISCSI详解(五)——ISCSI客户端配置实战
- 第五章-[bx]和Loop指令
- 2022.6.9-----leetcode. four hundred and ninety-seven
- 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
- Inheritance of row height
猜你喜欢

Chapter 8 - two basic problems of data processing
![[GUI development] browsing function implementation model of image processing software](/img/37/2162a6047682b9cfc9b8b7c2488068.jpg)
[GUI development] browsing function implementation model of image processing software

《MATLAB 神经网络43个案例分析》:第8章 GRNN网络的预测----基于广义回归神经网络的货运量预测

2022 low voltage electrician retraining question bank and online simulation examination

Set up redis sentinel cluster (instance):

【无标题】Task3 多路召回

Background position - exact units
![[untitled] task3 multiple recall](/img/84/81ada5dd7afac62c31a52e743547e9.png)
[untitled] task3 multiple recall

Engineers learn music theory (II) scale and tendency

Building a cluster: and replacing with error
随机推荐
Union selector
长安链节点证书、角色、权限管理介绍
Composition of box model
Regular verification user name
2022 simulated examination platform operation of high voltage electrician work license question bank
了结非对称密钥
ip、DNS、域名、URL、hosts
Chapter VI - procedures with multiple segments
最少换乘次数
Flink CheckPoint : Exceeded checkpoint tolerable failure threshold
抓取屏幕与毛玻璃效果
Background attribute compound writing
The classic dog contract of smart contract (I)
Detailed explanation of iSCSI (V) -- actual operation of iSCSI client configuration
node示例后台搭建
[character set 7] what are the wide character codes and multi byte codes of Chinese characters
《MATLAB 神经网络43个案例分析》:第8章 GRNN网络的预测----基于广义回归神经网络的货运量预测
You have an error in your SQL syntax; use near ‘and title=‘xxx‘‘ at line 5
svg中viewbox图解分析
第三章 寄存器 (内存访问)