class Solution {
public int longestConsecutive(int[] nums) {
if(nums.length==0)
return 0;
HashSet<Integer>set=new HashSet<Integer>();
for(int num:nums)
{
set.add(num);
}
int ans=1;
for(int num:nums)
{
int count=1;
if(!set.contains(num-1))
{
while(set.contains(num+1))
{
num++;
count++;
}
ans=Math.max(num,count);
}
}
return ans;
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter