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. java多线程编程核心技术——第一章总结

    目录: 1.1进程.多线程的概念,及线程的优点 1.2多线程的使用 1.3currentThread()方法 1.4isAlive()方法 1.5sleep()方法 1.6getId()方法 1.7停 ...

  2. navicat导入sql文件

    Hello,大家好.Navicat是我们平时使用较多的一个数据库客户端工具,平时小天我主要是用来连接mysql的,使用的时候还是很方便的. 今天小天我就给大家分享一个Navicat如何导入导出sql文 ...

  3. 使用PowerShell创建Azure Storage的SAS Token访问Azure Blob文件

    Azure的存储包含Storage Account.Container.Blob等具体的关系如下: 我们常用的blob存储,存放在Storage Account的Container里面. 目前有三种方 ...

  4. windows修改远程桌面RDP连接数

    windows 2003在默认情况下最多只允许两个用户进行远程终端连接,当达到两个远程桌面连接的到时候,再有人尝试连接,就会提示已经达到最大终端数,无法连上了. 一.windows2003终端连接数修 ...

  5. Tiny4412学习杂记

    1.Android 挂载NFS 使用 busybox mount 来替代mount命令 2.修改Uboot中fastboot最大buff  使用U-boot烧写Android5.0的时候出现 remo ...

  6. [51nod1113]矩阵快速幂

    解题关键:模板题,方便以后熟悉 #include<bits/stdc++.h> using namespace std; typedef long long ll; struct mat{ ...

  7. VMware安装Ubuntu时出现Intel VT-X处于禁用状态的情况的处理办法

    VMware安装Ubuntu时出现Intel VT-X处于禁用状态的情况的处理办法   VMware安装Ubuntu的出现Intel VT-X处于禁用状态的情况会使已经安装好的Ubuntu虚拟机打不开 ...

  8. Python开发【第二篇】: 基本数据类型(一)

    1. 整型   整型即整数,用 int 表示,在 Python3 中整型没有长度限制. 1.1 内置函数   1. int(num, base=None)   int( ) 函数用于将字符串转换为整型 ...

  9. linux---安装ftp并配置用户部分权限

    一.启动vsftpd服务1. 启动VSFTP服务器A:cenos下运行:yum install vsftpdB. 登录Linux主机后,运行命令:”service vsftpd start”C. 要让 ...

  10. CodeForces 104C【树特性】

    题意: 判断这副图是否满足根是一个环的缩点,然后其余点都是一个点,满足树特性. 思路: 一开始就像无向图缩点乱搞,然后实在太烦搞不下去.... 一副图是满足结点和边数量相等,且连通,则一定有一个环. ...