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.

===================

给一个未排序数组,找到一个最长的连续数组,返回其长度.

====

思路:

利用一个hash表,存储已经扫描的数组元素,

对数组中每一个curr元素,都必须在hash表中向前查找,向后查找,找出此nums[curr]过在连续数组。

maxlength = max(maxlength,length);

===

code:

  1. class Solution {
  2. public:
  3. int longestConsecutive(vector<int>& nums) {
  4. unordered_map<int,bool> hash_used;
  5. for(auto i:nums){
  6. hash_used[i] = false;
  7. }
  8. int longest = ;
  9. for(auto i:nums){
  10. if(hash_used[i])
  11. continue;
  12. int length = ;
  13.  
  14. hash_used[i] = true;
  15.  
  16. for(int j = i+;hash_used.find(j)!=hash_used.end();j++){
  17. hash_used[j] = true;
  18. length++;
  19. }
  20. for(int j = i-;hash_used.find(j)!=hash_used.end();j--){
  21. hash_used[j] = true;
  22. length++;
  23. }
  24. longest = max(longest,length);
  25. }//for
  26. return longest;
  27. }
  28. };

128. Longest Consecutive Sequence的更多相关文章

  1. 128. Longest Consecutive Sequence(leetcode)

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

  2. [LeetCode] 128. Longest Consecutive Sequence 解题思路

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

  3. 【LeetCode】128. Longest Consecutive Sequence

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

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

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

  5. 128. Longest Consecutive Sequence *HARD* -- 寻找无序数组中最长连续序列的长度

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

  6. leetcode 128. Longest Consecutive Sequence ----- java

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

  7. [leetcode]128. Longest Consecutive Sequence最长连续序列

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

  8. 128. Longest Consecutive Sequence最长连续序列

    [抄题]: Given an unsorted array of integers, find the length of the longest consecutive elements seque ...

  9. 128. Longest Consecutive Sequence (HashTable)

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

随机推荐

  1. 使用Jsoup函数包抓取网页内容

    之前写过一篇用Java抓取网页内容的文章,当时是用url.openStream()函数创建一个流,然后用BufferedReader把这个inputstream读取进来.抓取的结果是一整个字符串.如果 ...

  2. 移动端动画使用transform提升性能

    在移动端做动画,对性能要求较高而通常的改变margin属性是性能极低的,即使使用绝对定位改变top,left这些属性性能也很差因此应该使用transform来进行动画效果,如transform:tra ...

  3. 【转】非常适用的Sourceinsight插件,提高效率事半功倍

    原文网址:http://www.cnblogs.com/wangqiguo/p/3713211.html 一直使用sourceinsight编辑C/C++代码,sourceinsight是一个非常好用 ...

  4. 时事新闻之 谷歌 google 发布Tensor Flow 源代码

    TensorFlow: TensorFlow is an open source software library for numerical computation using data flow ...

  5. MUA

    a big deal analysis analytics cooperate 合作 efficient explicitly fine grained Granularity graph geogr ...

  6. JSBinding + SharpKit / 初体验:下载代码及运行Demo

    QQ群:189738580 以下是群主维护的JSB版本: git地址:https://github.com/qcwgithub/qjsbunitynew.git插件源码地址(不包含SpiderMonk ...

  7. CUDA中的流与事件

    流:CUDA流很像CPU的线程,一个CUDA流中的操作按顺序进行,粗粒度管理多个处理单元的并发执行. 通俗的讲,流用于并行运算,比如处理同一副图,你用一个流处理左边半张图片,再用第二个流处理右边半张图 ...

  8. Linux-SSL和SSH和OpenSSH,OpenSSL有什么区别

    ssl是通讯链路的附加层.可以包含很多协议.https, ftps, ..... ssh只是加密的shell,最初是用来替代telnet的.通过port forward,也可以让其他协议通过ssh的隧 ...

  9. python datetime

    不管何时何地,只要我们编程时遇到了跟时间有关的问题,都要想到 datetime 和 time 标准库模块,今天我们就用它内部的方法,详解python操作日期和时间的方法.1.将字符串的时间转换为时间戳 ...

  10. unity, ugui input field

    ugui Input Field,获取输入的字符串. 错误方法: string content=inputField.FindChild("Text").text; 这样得到的是输 ...