Longest Consecutive Sequence(未)

来源:互联网 发布:软件评测师真题2015 编辑:程序博客网 时间:2024/06/06 09:54

题目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都会自动对元素排序,排序复杂度就会上去。
先写到这里,回来来补充。

0 0
原创粉丝点击