forked from BigEggStudy/LeetCode-CS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0128-LongestConsecutiveSequence.cs
34 lines (30 loc) · 1.03 KB
/
0128-LongestConsecutiveSequence.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//-----------------------------------------------------------------------------
// Runtime: 96ms
// Memory Usage: 24.8 MB
// Link: https://leetcode.com/submissions/detail/380542728/
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
namespace LeetCode
{
public class _0128_LongestConsecutiveSequence
{
public int LongestConsecutive(int[] nums)
{
var hashSet = new HashSet<int>(nums);
var longestStreak = 0;
for (int i = 0; i < nums.Length; i++)
if (!hashSet.Contains(nums[i] - 1))
{
int currentNum = nums[i], currentStreak = 1;
while (hashSet.Contains(currentNum + 1))
{
currentNum += 1;
currentStreak++;
}
longestStreak = Math.Max(longestStreak, currentStreak);
}
return longestStreak;
}
}
}