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. 拓扑排序 POJ 1094 Sorting It All Out

    题意:给定N个字和M行他们之间的关系,要求输出他们的拓扑排序.此题采用边输入边检测的方式,如果发现环,就结束并输出当前行号:如果读取到当前行时,可以确定拓扑序列就输出,不管后面的输入(可能包含环路): ...

  2. kvm 基础 虚拟机改名

    转自:http://www.cnblogs.com/5201351/p/4464350.htm 1.查看所有的kvm虚拟机 [root@5201351_kvm ~]# virsh list --all ...

  3. go 语言 基础 类型(1)

    变量 使用关键字 var定义变量,自动初始化为0值.如果提供初始化值,可省略变量类型,由编译器自动推断. 在函数内部可以使用 := 方式定义变量 func main() { x := 123 } 可一 ...

  4. 利用java mail发送邮件

    import java.util.Date; import java.util.Properties; import javax.activation.DataHandler; import java ...

  5. 13、ubuntu终端快捷键(参考 dy9776)

    1.终端的快捷键 Tab 自动补全 Ctrl+a 光标移动到开始位置 Ctrl+e 光标移动到最末尾 Ctrl+l 相当于clear,即清屏 Ctrl+Z 把当前任务放到后台运行(相当于运行命令时后面 ...

  6. nfs使用教程

    服务端 配置说明 文件名 /etc/exports 格式 <输出目录> [客户端 (访问权限,用户映射,其他) ] [客户端 (访问权限,用户映射,其他) ] 格式说明 输出目录:NFS系 ...

  7. LOJ6053 简单的函数(min_25筛)

    题目链接:LOJ 题目大意:从前有个积性函数 $f$ 满足 $f(1)=1,f(p^k)=p\oplus k$.(异或)求其前 $n$ 项的和对 $10^9+7$ 取模的值. $1\le n\le 1 ...

  8. ABC118D(DP,完全背包,贪心)

    #include<bits/stdc++.h>using namespace std;int cnt[10]={0,2,5,5,4,5,6,3,7,6};int dp[10007];int ...

  9. RHEL 7.3 无图形界面安装oracle 11gr2

    行方服务器无X界面,只能使用纯命令安装方式.提供RHEL全量系统镜像. 在网上搜了搜,参照CSDN作者“西伯利亚疯狂的蚯蚓”的安装过程成功安装oracle11gR2 原文:https://blog.c ...

  10. 洛谷P3006 [USACO11JAN]瓶颈Bottleneck(堆模拟)

    传送门 感觉这题的思路还是挺不错的.然而为啥全网就一个题解而且只有代码……然后我只好看着代码理解了好久…… 题意就是有一棵树,每一个节点向他父亲节点连边,且有一个容量表示每一秒可以经过的牛的数量,每一 ...