Longest Consecutive Sequence
来源:互联网 发布:数据库结构的三个模式 编辑:程序博客网 时间:2024/03/29 15:27
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.
思路一:把数据放在set中达到找每个数字的下一个连续数字只需要O(1)的目的。然后对于每一个元素找到所有连续后继,直到set中不存在它可以链到的后继,对于访问过的数字,从set中移除,因为比较大的数字的连续后继数的数目肯定小于它的前驱数字的,所以不需要重复计算。而对于某个数字,如”链到“的某个后继的连续后继数字的数目已知,就可以直接用当前计算到的”链长“加上后继的连续后继数字的数目得到,这样,虽然代码看似有两重循环,实际每个数字只会被访问一次,所以是O(n)复杂度的。
代码如下:
class Solution { public: int longestConsecutive(vector<int> &num) { // Start typing your C/C++ solution below // DO NOT write int main() function unordered_set<int> num_set; num_set.insert(num.begin(),num.end()); map<int,int> next_num_map; int max_len = 0; unordered_set<int>::iterator it1 = num_set.begin(); while(it1 != num_set.end()) { int cnt = 0; int number = *it1; int number2 = number; unordered_set<int>::iterator it2 = num_set.find(++number); while( it2 != num_set.end()) { cnt++; num_set.erase(number); map<int,int>::iterator it3 = next_num_map.find(number); if( it3 != next_num_map.end()) { cnt += it3->second; break; } it2 = num_set.find(++number); } next_num_map.insert(pair< int,int >(number2,cnt)); max_len = max(max_len,cnt+1); it1++; } return max_len; } };
72 milli secs
方法二:use HashSet, pick one element V from Set, check whether V+1 and V-1 is in Set
class Solution { public: int longestConsecutive(vector<int> &num) { // Start typing your C/C++ solution below // DO NOT write int main() function unordered_set<int> s(num.begin(), num.end()); int max_length = 0; while(!s.empty()) { int value = *s.begin(); int upper_bound = value; int lower_bound = value; int length = 1; s.erase(value); while(s.find(upper_bound+1) != s.end()) { upper_bound++; length++; s.erase(upper_bound); } while(s.find(lower_bound-1) != s.end()) { lower_bound--; length++; s.erase(lower_bound); } if (length > max_length) { max_length = length; } } return max_length; } };
60 milli secs
更简单的代码,回头看一下
http://blog.sina.com.cn/s/blog_b9285de20101iqar.html
- Longest Consecutive Sequence
- Longest Consecutive Sequence
- Leetcode : Longest Consecutive Sequence
- [Leetcode] Longest Consecutive Sequence
- longest consecutive elements sequence
- [leetcode] longest consecutive sequence
- Longest Consecutive Sequence
- Longest Consecutive Sequence
- 【leetcode】Longest Consecutive Sequence
- LeetCode - Longest Consecutive Sequence
- [LeetCode]Longest Consecutive Sequence
- leetcode:Longest Consecutive Sequence
- Longest Consecutive Sequence
- Longest Consecutive Sequence
- [Leetcode]Longest Consecutive Sequence
- [leetcode]Longest Consecutive Sequence
- Longest Consecutive Sequence
- Longest Consecutive Sequence
- MYSQL 数据库乱码
- 我们该如何设计数据库(四)
- 谈谈常见的移动应用设计风格
- 1006 ZOJ问题 九度Online Judge
- 爱的真谛
- Longest Consecutive Sequence
- java第十一天_线程2
- 类的构造
- sharekit简单使用说明
- 程序员_Java初级<一> 概述
- 第一节:JAVA概述
- java.lang.RuntimeException: Unable to instantiate activity ComponentInfo异常解决
- 计算不确定度的代码,老师再也不用担心我算错结果了= =DEBUG中……
- Linux内存管理