并查集,遍历数组,如果该数字+1在数组中则将它们合并。
class Solution {
unordered_map<int,int> father,cnt;
int Find(int x)
{
int r = x;
while(r!=father[r])
{
r = father[r];
}
int i = x;
int j;
while(father[i]!=i)
{
j = father[i];
father[i] = r;
i = j;
}
return r;
}
int Union(int x, int y)
{
int fx = Find(x);
int fy = Find(y);
if(fx==fy)
return cnt[fx];
if(fx<fy)
{
father[fy] = fx;
cnt[fx] += cnt[fy];
return cnt[fx];
}
else {
father[fx] = fy;
cnt[fy] += cnt[fx];
return cnt[fy];
}
}
public:
int longestConsecutive(vector<int>& nums) {
int N = nums.size();
if(N<1)
return 0;
for(auto x:nums)
{
father[x] = x;
cnt[x] = 1;
}
int ans = 1;
for(auto x : nums)
{
if(father.find(x+1)!=father.end())
{
ans = max(ans,Union(x,x+1));
}
}
return ans;
}
};
使用集合。
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
if(nums.size()==0)
return 0;
unordered_set<int> st;
for(int x:nums)
{
st.insert(x);
}
int ans = 1;
for(int x:nums)
{
if(st.count(x-1))
continue;
int cnt = 1;
int t = x;
while(st.count(t+1))
{
cnt++;
t++;
}
ans = max(ans,cnt);
}
return ans;
}
};