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:

class Solution {
public:
int longestConsecutive(vector<int>& nums) {
unordered_map<int,bool> hash_used;
for(auto i:nums){
hash_used[i] = false;
}
int longest = ;
for(auto i:nums){
if(hash_used[i])
continue;
int length = ; hash_used[i] = true; for(int j = i+;hash_used.find(j)!=hash_used.end();j++){
hash_used[j] = true;
length++;
}
for(int j = i-;hash_used.find(j)!=hash_used.end();j--){
hash_used[j] = true;
length++;
}
longest = max(longest,length);
}//for
return longest;
}
};

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. twisted的defer模式和线程池

    前言: 最近帮朋友review其模块服务代码, 使用的是python的twisted网络框架. 鉴于之前并没有使用过, 于是决定好好研究一番. twisted的reactor模型很好的处理了网络IO事 ...

  2. android默认浏览器response下载PDF文件

    下载出来的文件不是PDF,而是xxx.htm文件,原因是response的header配置有问题. android默认浏览器的情况下,header的配置应该写成.(java 为例) response. ...

  3. php部分---include()与require()的区别、empty()与isset is_null的区别与用法详解

    include()与require()的用途是完全一样的,不一定非得哪个放在最前面哪个放在中间.他们最根本的区别在于错误处理的方式不一样. 1.处理错误的方式: require()一个文件存在错误的话 ...

  4. Codeforces Round #123 (Div. 2)

    A. Let's Watch Football 记时间为\(t\), 则\(bt+(b-a)c>=0\),可得\[t \ge \frac{c(a-b)}{b}\] B. After Traini ...

  5. php归获取当前目录下的二级目录数 和文件数

    <?php        header('Content-Type: text/html; charset=gb2312');        // $baseDir = "/www/u ...

  6. spark优化之优化数据结构

    概序: 要减少内存的消耗,除了使用高效的序列化类库以外,还有一个很重要的事情,就是优化数据结构.从而避免Java语法特性中所导致的额外内存的开销,比如基于指针的Java数据结构,以及包装类型. 有一个 ...

  7. POJ-3162 Walking Race (求树上两点之间最大距离)

    题目大意:给一棵树,对于所有的点,找出距它最远点的距离,然后将这些距离排成一列,找出最长的一个区间满足:其中的最大值减去最小值不大于m. 题目分析:两次dfs找出距每个节点的最远距离,然后可以通过维护 ...

  8. java多线程之:深入JVM锁机制2-Lock (转载)

    前文(深入JVM锁机制-synchronized)分析了JVM中的synchronized实现,本文继续分析JVM中的另一种锁Lock的实现.与synchronized不同的是,Lock完全用Java ...

  9. COM ,Threading Models,apartments,RPC

    Component Object Model (COM) https://msdn.microsoft.com/en-us/library/windows/desktop/ms680573%28v=v ...

  10. 在Hyper-V虚拟机中使用Wi-Fi上网

    笔记本配置了一块以太网卡和一块无线网卡.由于平时常用Wi-Fi上网,偶然发现Hyper-V虚拟机默认不能使用宿主系统的无线网卡上网,据说是出于安全方面的考虑.后来参考"Using Wirel ...