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.

这题自己只做出个逗比版,用位向量来先标记一遍,然后在遍历位向量算出最长,这相当于是排序,不过空间消耗可能很大。。。因为位向量的大小是数组里最大那个元素,这个方法不能处理很大元素的情况,如果数组有个元素是100000,但是数组大小只是5,位向量依然要100000这么大。。。而且也不能处理负数的情况,所以是逗比版。

正常的解法就是所谓的空间换时间,用哈希表先把数组存下来耗时O(n),然后去遍历哈希表,拿出一个数然后把他升序和降序的连续数找出来移除并记录下他们的长度,然后与最大值比较并更新,这样当整个哈希表为空的时候最大值也找到了,复杂度亦是O(n)

  1. int consecutive(unordered_set<int>& set, int value, bool asc) {
  2. int cnt = ;
  3. while (set.find(value) != set.end()) {
  4. cnt++;
  5. set.erase(value);
  6. if (asc) {
  7. value++;
  8. }else {
  9. value--;
  10. }
  11. }
  12. return cnt;
  13. }
  14.  
  15. int longestConsecutive(vector<int> &num) {
  16. int max = ;
  17. unordered_set<int> set;
  18. for (int n: num) {
  19. set.insert(n);
  20. }
  21. for (int i = ; i < num.size(); i++) {
  22. int value = num[i];
  23. int seq = consecutive(set, value, true) + consecutive(set, value-, false);
  24. if (seq > max) max = seq;
  25. }
  26. return max;
  27. }

另附逗比版...

  1. int longestConsecutive_kidding(vector<int> &num) {
  2. int max = ;
  3. for (int i=; i<num.size(); i++) {
  4. if (max < num[i]) max = num[i];
  5. }
  6. vector<int> pos(max);
  7.  
  8. for (int i = ; i < max; i++) {
  9. pos[i] = ;
  10. }
  11.  
  12. for (int i = ; i < num.size(); i++) {
  13. pos[num[i]] = ;
  14. }
  15.  
  16. int j = , l = ;
  17. for (int i = ; i < max; i++) {
  18. if (pos[i] == ) {
  19. j++;
  20. if (j > l) l = j;
  21. }
  22. else {
  23. j = ;
  24. }
  25. }
  26.  
  27. return l;
  28. }

逗比

[LeetCode] Longest Consecutive Sequence的更多相关文章

  1. LeetCode——Longest Consecutive Sequence

    LeetCode--Longest Consecutive Sequence Question Given an unsorted array of integers, find the length ...

  2. [LeetCode] Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  3. LeetCode: Longest Consecutive Sequence 解题报告

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

  4. [leetcode]Longest Consecutive Sequence @ Python

    原题地址:https://oj.leetcode.com/problems/longest-consecutive-sequence/ 题意: Given an unsorted array of i ...

  5. LeetCode: Longest Consecutive Sequence [128]

    [题目] Given an unsorted array of integers, find the length of the longest consecutive elements sequen ...

  6. Leetcode: Longest Consecutive Sequence && Summary: Iterator用法以及ConcurrentModificationException错误说明

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  7. LeetCode—Longest Consecutive Sequence

    题目描述: Given an unsorted array of integers, find the length of the longest consecutive elements seque ...

  8. [Leetcode] Longest consecutive sequence 最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  9. [Leetcode] Longest Consecutive Sequence 略详细 (Java)

    题目参见这里 https://leetcode.com/problems/longest-consecutive-sequence/ 这个题目我感觉很难,看了半天别人写的答案,才明白个所以然.下面的代 ...

随机推荐

  1. Python自动化之线程进阶篇(二)

    queue队列 class queue.Queue(maxsize=0) #先入先出 class queue.LifoQueue(maxsize=0) #后入先出 class queue.Priori ...

  2. matplotlib绘制直方图【柱状图】

    代码: def drawBar(): xticks = ['A', 'B', 'C', 'D', 'E']#每个柱的下标说明 gradeGroup = {'A':200,'B':250,'C':330 ...

  3. excle导入

    public function import_upload(){ set_time_limit(900); if(!empty($_FILES ['xls_path']['name'])){ $tmp ...

  4. 【Kubernetes】两篇文章 搞懂 K8s 的 fannel 网络原理

    近期公司的flannel网络很不稳定,花时间研究了下并且保证云端自动部署的网络能够正常work. 1.网络拓扑 拓扑如下:(点开看大图)  容器网卡通过docker0桥接到flannel0网卡,而每个 ...

  5. c++面试题目解析

    1.指针和引用的区别 答:A.指针可修改,引用不可改. B.指针占用内存,引用不占内存. C.引用不能为空   指针可以为空. 2.memcpy和strcpy的区别 答:strcpy  会拷贝到\0结 ...

  6. MySQL中的增删改查

    将表cm_application中的state字段类型改为字符串型 alter table  cm_application  modify STATE varchar(50); 将表cm_applic ...

  7. Solr集群更新配置的方式

    solr集群中配置文件是经常更新的,频率最高的也就是schema.xml和solrconfig.xml这两个配置文件了,对于更新配置文件之前,我们先了解一下集群项目结构 由于在集群模式下,solrco ...

  8. JAVA手记 JAVA入门(安装+Dos下运行)

    JAVA入门特供= =,今天设置环境变量后用dos运行的时候发现出现“找不到或无法加载主类”,索性查了些资料重新看了看JAVA入门的部分. 声明:我的笔记本暂时用的是Win10系统,Windows其他 ...

  9. [转]Git远程操作详解

    原文:http://www.ruanyifeng.com/blog/2014/06/git_remote.html Git是目前最流行的版本管理系统,学会Git几乎成了开发者的必备技能. Git有很多 ...

  10. IOS- 数据存储

    在iOS开发过程中,不管是做什么应用,都会碰到数据保存的问题.将数据保存到本地,能够让程序的运行更加流畅,不会出现让人厌恶的菊花形状,使得用户体验更好.下面介绍一下数据保存的方式: 1.NSKeyed ...