您的位置:首页 > 产品设计 > UI/UE

Longest Consecutive Sequence(未)

2015-08-02 15:33 399 查看

题目128:Longest Consecutive Sequence

题目描述:

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.

题目分析:

在一个没有排序的整型数组中,找到长度最长的顺序连续的子序列,举例的很清楚,要求时间复杂度是O(n) 。

思路分析:

如果时间复杂度没有要求,则先对数组排序,排序后遍历,但排序的时间复杂度是O(nlog n),不符合要求;

有看过别人的解法,有用C++ STL中的set,set和multiset都会自动对元素排序,排序复杂度就会上去。

先写到这里,回来来补充。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: