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实现

使用一个集合set存入所有的数字,然后遍历数组中的每个数字,如果其在集合中存在,那么将其移除,然后分别用两个变量pre和next算出其前一个数跟后一个数,然后在集合中循环查找,如果pre在集合中,那么将pre移除集合,然后pre再自减1,直至pre不在集合之中,对next采用同样的方法,那么next-pre-1就是当前数字的最长连续序列,更新res即可。

 class Solution {
public:
int longestConsecutive(vector<int>& nums) {
if(nums.size()==||nums.empty())
return ;
unordered_set<int> s(nums.begin(),nums.end());
int res=;
for(int val:nums)
{
if(!s.count(val))
continue;
s.erase(val);
int pre=val-,next=val+;
while(s.count(pre))
s.erase(pre--);
while(s.count(next))
s.erase(next++);
res=max(res,next-pre-);
}
return res;
}
};

方法二:map实现

刚开始哈希表为空,然后遍历所有数字,如果该数字不在哈希表中,那么我们分别看其左右两个数字是否在哈希表中,如果在,则返回其哈希表中映射值,若不在,则返回0,然后我们将left+right+1作为当前数字的映射,并更新res结果,然后更新d-left和d-right的映射值。

 class Solution {
public:
int longestConsecutive(vector<int>& nums) {
if(nums.size()==||nums.empty())
return ;
unordered_map<int,int> m;
int res=;
for(int val:nums)
{
if(!m.count(val))
{
int left=m.count(val-)?m[val-]:;
int right=m.count(val+)?m[val+]:;
int sum=left+right+;
res=max(res,sum);
m[val]=sum;
m[val-left]=sum;
m[val+right]=sum;
}
}
return res;
}
};

LeetCode 128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列的更多相关文章

  1. 128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列

    给定一个未排序的整数数组,找出最长连续序列的长度.例如,给出 [100, 4, 200, 1, 3, 2],这个最长的连续序列是 [1, 2, 3, 4].返回所求长度: 4.要求你的算法复杂度为 O ...

  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 求最长连续序列

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

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

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

  5. Java for LeetCode 128 Longest Consecutive Sequence

    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 (union find)

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

  8. Java算法-------无序数组中的最长连续序列---------leetcode128

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

  9. Leetcode#128 Longest Consecutive Sequence

    原题地址 1. 把所有元素都塞到集合里2. 遍历所有元素,对于每个元素,如果集合里没有,就算了,如果有的话,就向左向右拓展,找到最长的连续范围,同时在每次找的时候都把找到的删掉.这样做保证了同样的连续 ...

随机推荐

  1. 安装LMS记

    LMS学习管理系统,即英文Learning Management System的缩写.中文常用别名:在线学习系统. 尝试安装一款LMS,并进行二次开发. Moodle 首先想到的是Moodle.Moo ...

  2. Java中Redis入门(1)

    Redis是一个开源的,先进的 key-value 存储可用于构建高性能,可扩展的 Web 应用程序的解决方案. Redis官方网网站是:http://www.redis.io/,如下: Redis ...

  3. LAMP 1.4 PHP编译安装

    1.下载                                                                                                 ...

  4. centos6.5安装dubbo管控台教程(四)

    阅读此文之前,需要先安装zookeeper. 阅读文章: http://www.cnblogs.com/duenboa/articles/6665169.html   1. 下载文件 dubbo-ad ...

  5. Maven jenkins +Jmeter自动化测试

    Maven jenkins +Jmeter自动化测试 1. Jenkins中集成jmeter-maven插件 http://my.oschina.net/u/1377774/blog/168969 2 ...

  6. ansible的使用

    ansible主要分为单条命令和组命令(即配置后缀为名yml的文件,使用ansible-playbook执行)的使用,以下分别对两者进行说明.如不会安装ansible,请参考我的文章 centos7安 ...

  7. 实用掌中宝--HTML&CSS常用标签速查手册 PDF扫描版

    实用掌中宝--HTML&CSS常用标签速查手册 内容推荐: 本书第一篇以语法和实例相结合的形式,详细讲解了HTML语言中各个元素及其属性的作用.语法和显示效果:第二篇从CSS基本概念开始,分别 ...

  8. NIO 之阻塞IO和非阻塞IO(转载)

    阻塞模式 IO 我们已经介绍过使用 Java NIO 包组成一个简单的客户端-服务端网络通讯所需要的 ServerSocketChannel.SocketChannel 和 Buffer,我们这里整合 ...

  9. Boost Python官方样例(三)

    导出C++类(纯虚函数和虚函数) 大致做法就是为class写一个warp,通过get_override方法检测虚函数是否被重载了,如果被重载了调用重载函数,否则调用自身实现,最后导出的时候直接导出wa ...

  10. Python实现返回指定范围内的所有素数

    # 获取a, b范围的所有素数 def func(a, b): li = [] for i in range(a, b+1): for j in range(2, i): if i % j == 0: ...