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(1),遍历到某个数据,可以向前和向后找它的连续序列。再用一个哈希表存储已访问过的元素,这样保证每个元素至多被处理一次。

哈希表在C++中用unordered_set实现。set的实现是红黑树,插入查找删除的时间复杂度是O(logn)不能使用。

class Solution {
public:
int longestConsecutive(vector<int>& nums) {
unordered_set<int> visited;
unordered_set<int> exist;
int ret = ;
int count;
int target; for(int i = ; i < nums.size(); i++){
exist.insert(nums[i]);
} for(int i = ; i < nums.size(); i++){
if(visited.find(nums[i])!=visited.end()) continue; visited.insert(nums[i]);
count = ;
target = nums[i];
while(exist.find(--target)!=visited.end()){
visited.insert(target);
count++;
}
target = nums[i];
while(exist.find(++target)!=visited.end()){
visited.insert(target);
count++;
}
if(count > ret) ret = count;
}
return ret;
}
};

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

  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. 128. Longest Consecutive Sequence

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

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

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

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

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

随机推荐

  1. lapis 路由

    1. 路由以及url 模式 参考如下: local lapis = require("lapis") local app = lapis.Application() app:mat ...

  2. 在 CentOS 7.2 上安装 ODOO 10 (2018-10-09 持续更新)

    在 CentOS 7.2 上安装 ODOO 10 更新系统 yum update 安装 EPEL 源 1 yum install -y epel-release 安装依赖组件 yum install ...

  3. 本地tomcat调用远程接口报错:java.lang.reflect.InvocationTargetException

    今天碰到一个奇怪的问题,本地Eclipse起了一个tomcat通过http去调一个外部接口,结果竟然报了一个反射的异常,先看下完整日志: , :: 下午 org.apache.catalina.sta ...

  4. C#开机启动与退出程序

    最新用到的项目中需要使用开机自启和退出程序,于是需要 http://www.cnblogs.com/Gaoswatou/p/6605760.html C# WinForm程序退出的方法 1.this. ...

  5. Zen Coding改名Emmet-功能更智能化

    早在2009年,谢尔盖Chikuyonok写了一篇文章,提出了一种新的编写HTML和CSS代码的方式.这一革命性的插件,被称为zen coding,多年来已帮助许多开发人员,现在已达到一个新的水平. ...

  6. one2many &&many2many

    只记录双向的情况(双向是单向的一种)  @OneToMany 和 @ManyToOne :一个Group 包含多个 User; Group.class package com.XX.model; im ...

  7. Hive中的用户自定义函数UDF

    Hive中的自定义函数允许用户扩展HiveQL,是一个非常强大的功能.Hive中具有多种类型的用户自定义函数.show functions命令可以列举出当前Hive会话中的所加载进来的函数,包括内置的 ...

  8. hadoop集群调优-OS和文件系统部分

    OS and File System 根据Dell(因为我们的硬件采用dell的方案)关于hadoop调优的相关说明,改变几个Linux的默认设置,Hadoop的性能能够增长大概15%. open f ...

  9. (转) Docker EE/Docker CE简介与版本规划

    随着Docker的不断流行与发展,docker公司(或称为组织)也开启了商业化之路,Docker 从 17.03版本之后分为 CE(Community Edition) 和 EE(Enterprise ...

  10. 【BZOJ】2818: Gcd(欧拉函数+质数)

    题目 传送门:QWQ 分析 仪仗队 呃,看到题后感觉很像上面的仪仗队. 仪仗队求的是$ gcd(a,b)=1 $ 本题求的是$ gcd(a,b)=m $ 其中m是质数 把 $ gcd(a,b)=1 $ ...