当前位置:网站首页>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;
}
边栏推荐
- Engineers learn music theory (I) try to understand music
- Wechat applet image saving function
- MFS详解(四)——MFS管理服务器安装与配置
- sql中的Exists用法
- API handling Android security distance
- [advanced pointer I] character array & array pointer & pointer array
- 清华大学数据挖掘笔记(一)
- POI library update excel picture
- ERROR 1630 (42000): FUNCTION a.avg does not exist. Check the ‘Function Name Parsing and Resolution‘
- 《MATLAB 神经网络43个案例分析》:第8章 GRNN网络的预测----基于广义回归神经网络的货运量预测
猜你喜欢

Jupyter notebook sets the default browser to open with an error syntaxerror: (Unicode error) 'UTF-8' codec can't decode byte 0xd4

2022 melting welding and thermal cutting test questions and answers

Union selector

Handling abnormal data
![[essence] explain in detail the memory management mechanism in QT](/img/7d/0d83158c6b0574dd3b3547b47af67e.jpg)
[essence] explain in detail the memory management mechanism in QT

Unittest测试框架

ip、DNS、域名、URL、hosts

Background location case II

【数据存储】浮点型数据在内存中的存储

利用nvm动态调整nodejs版本,解决因为node版本过高或过低导致项目无法运行和打包
随机推荐
Flink passes in custom parameters or profiles
Chapter V -[bx] and loop instructions
处理异常数据
[advanced pointer I] character array & array pointer & pointer array
通俗理解时域采样与频域延拓
RuntimeError:Input and parameter tensors are not at the same device, found input tensor at cuda:0 an
Unittest测试框架
Background location case II
[computer use] how to change a computer disk into a mobile disk?
Chapter 7 - more flexible location of memory addresses
Analysis of 43 cases of MATLAB neural network: Chapter 8 prediction of GRNN Network - Freight Volume Prediction Based on generalized regression neural network
Knee joint
Introduction to applet cloud development -- questionnaire evaluation applet practice (7)
第四章-第一个程序
Chapter 8 - two basic problems of data processing
最少换乘次数
(十五) TweenRunner
Loading font component loading effect
Engineers learn music theory (II) scale and tendency
Display the remaining valid days according to the valid period