LeetCode 128 Longest Consecutive Sequence 题解
1.题目:
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
For example,
Given [100, 4, 200, 1, 3, 2],
The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.
Your algorithm should run in O(n) complexity.
Subscribe to see which companies asked this question
题意:
给一个乱序的数组,找出这个数组里连续的最长长度。
2.解题思路:
先用一个HashSet除去重复的值,再用一个HashSet从某点开始延展开来,记录两个端点,两个端点相减即为连续的长度,记录最大值。
3.代码
1 |
|