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.
思路:
这题首先想到的是,利用数组将num中的每个数字当做index放入一个新的数组中,然后寻找连续的填充过的范围即可。
但是,题目中并没有告诉你数字的范围,如果类似于 [1,100000,-10000]这样的num,那么就没法做了。首先,数字范围......
阅读全文