方法一:使用快排:

//排序法,时间O(nlogn),使用STL,只是验证一下思想,非正解;
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
sort(nums.begin(),nums.end());
int res=;
for(int i=;i<nums.size();i++){
int step=,len=;
while(i+step!=nums.size()-&&nums[i+step+]-nums[i+step]<=){
if(nums[i+step]+==nums[i+step+]) len++;
step++;
}
res=max(res,len);
i+=step;
}
return res;
}
};

方法二:使用并查集如题所说达到O(n)

方法三:使用哈希表O(n)

//哈希表结合染色,建立一个哈希表,然后遍历之后计数每个元素周围所有相邻元素并染色,记录个数;O(n)复杂度
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
int len=nums.size();
if(len<=) return len;
unordered_map<int,int> m;
int res=;
for(int n:nums)
m[n]=;
for(int n:nums){
int i=n,j=n;
int cnt=;
if(m[n]==)
continue;
else
m[n]=;
while(m[i+]==){
i++;
m[i]=;
} while(m[j-]==){
j--;
m[j]=;
} cnt=i-j+;
res=cnt>res?cnt:res;
}
return res;
}
};

别人家的哈希表:

/****
通过哈希表记录边界信息 neither i+1 nor i-1 has been seen: m[i]=1; both i+1 and i-1 have been seen: extend m[i+m[i+1]] and m[i-m[i-1]] to each other; only i+1 has been seen: extend m[i+m[i+1]] and m[i] to each other; only i-1 has been seen: extend m[i-m[i-1]] and m[i] to each other. *****/
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
int len=nums.size();
if(len<=) return len;
unordered_map<int,int> m;
int res=;
for(int i:nums){
if(m[i]) continue;
//后面表达式为将m[i]和这一段连续序列的边界全部赋值为他的长度
//边界只可能为m[i-m[i-1]]到m[i+m[i+1]],m[i]到m[i+m[i+1]],m[i-m[i-1]]到m[i]这几种情况,因此更新三者的值为新连续序列的长度即可
//又因为没有的元素哈希值为0,所以m[i]左右元素的m[i-1]+m[i+1]+1为新序列的长度;
res=max(res,m[i]=m[i+m[i+]]=m[i-m[i-]]=m[i-]+m[i+]+);
}
return res;
}
};

别人家的使用hashset 和 hashtable:

hashset:

/****
通过哈希set 将nums转化为哈希set,然后对哈希set进行遍历,寻找连续片段的左边界,然后num+1进行遍历。
可以证明每个元素将被访问2遍,for中一遍,while一遍,所以time O(n),space O(n);
由于int会产生越界,可以使用long,也可以进行边界检测,INT_MAX break; *****/
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
int res=;
unordered_set<int> h(nums.begin(),nums.end());
for(int num:nums){
int l=;
if(!h.count(num-)){
while(h.count(num)!=){
++l;
if(num==INT_MAX) break;
++num;
}
res=res>l?res:l;
}
}
return res;
}
};

hashtable:

/****
通过哈希table solution 1: hashtable (key,len)
case1: no neighboors
h[num]=1;
case2: one neighboor
l=h[num-1] or r=h[num+1]
h[num]=h[num-1]=l+1 or h[num]=h[num+1]=r+1
case3: two neighboors
l=h[num-1]
r=h[num+1]
h[num]=h[num-1]=h[num+1]=l+r+1 *****/
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
int res=;
unordered_map<int,int> h;
for(int num:nums){
if(h[num]!=) continue;
int l=h[num-];
int r=h[num+]; int t=l+r+; h[num]=h[num+r]=h[num-l]=t;
res=res>t?res:t;
}
return res;
}
};

哈希set

/****
通过哈希set 将nums转化为哈希set,然后对哈希set进行遍历,寻找连续片段的左边界,然后num+1进行遍历。
可以证明每个元素将被访问2遍,for中一遍,while一遍,所以time O(n),space O(n);
由于int会产生越界,可以使用long,也可以进行边界检测,INT_MAX break; *****/
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
int res=;
unordered_set<int> h(nums.begin(),nums.end());
for(int num: nums){
int l=;
if(h.count(num-)==){
while(h.count(num)>){
l++;
if(num==INT_MAX) break;
num++;
}
}
res=res>l?res:l;
}
return res;
}
};
有道词典

solution 1: has ...

详细X

  解决方案1:哈希表(关键,len) case1:没有neighboorsh (num) = 1,例2:一个neighboorl = h [num-1]或r = h (num + 1) h (num) = h [num-1] = l + 1或h (num) = h (num + 1) = r + 1 case3:两个neighboorsl = h [num-1] r = h (num + 1) h (num) = h [num-1] = h (num + 1) = l + r + 1
  
  
  * * * * * /类解决方案{公众:int longestConsecutive(向量< int > & num) {int res = 0; unordered_map < int, int > h;为(int num: num){如果(h (num) ! = 0)继续;int l = h [num-1]; int r = h (num + 1); int t = l + r + 1; h (num) = h (num + r) = h [num-l] = t; res = res > t ? res: t;}返回res;}};

leetcode 128最长连续序列的更多相关文章

  1. Java实现 LeetCode 128 最长连续序列

    128. 最长连续序列 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连 ...

  2. 图解leetcode —— 128. 最长连续序列

    前言: 每道题附带动态示意图,提供java.python两种语言答案,力求提供leetcode最优解. 描述: 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). ...

  3. LeetCode 128. 最长连续序列(Longest Consecutive Sequence)

    题目描述 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连续序列是 [1 ...

  4. 【LeetCode】128. 最长连续序列

    题目 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为O(n). 示例: 输入:[100, 4, 200, 1, 3, 2] 输出:4 解释:最长连续序列是[1, 2, 3, ...

  5. leetcode.哈希表.128最长连续序列-Java

    1. 具体题目 给定一个未排序的整数数组,找出最长连续序列的长度.要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连续序列是 ...

  6. leetcode 128. 最长连续子序列

    题目描述: 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入:[100, 4, 200, 1, 3, 2] 输出:4 即最长的连续序列为 [1,2, ...

  7. 【LeetCode】最长连续序列

    [问题]给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [, , , , , ] 输出: 解释: 最长连续序列是 [, , , ].它的长度为 ...

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

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

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

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

随机推荐

  1. contenteditable兼容问题

    正常情况下用contenteditable,IE下有兼容性问题需要将个别字母变成大写的contentEditable. 获取contenteditable的内容 对html进行处理 兼容 chrome ...

  2. Linux内核链表深度分析

    链表简介:链表是一种常用的数据结构,它通过指针将一系列数据节点连接成一条数据链.相对于数组,链表具有更好的动态性,建立链表时无需预先知道数据总量,可以随机分配空间,可以高效地在链表中的任意位置实时插入 ...

  3. python-1.Centos7安装Python3.6和Scrapy的方法

    由于centos7原本就安装了Python2,而且这个Python2不能被删除,因为有很多系统命令,比如yum都要用到 [root@iZm5efjrz9szlsq1a0ai3gZ ~]# python ...

  4. 孕期出血是否先兆流产——B超看婴儿是否在子宫内+hcg值是否过低孕激素不足

    转自:http://blog.sina.com.cn/s/blog_4a869c130102e7nu.html 很多人都经历过孕早期阴道出血,但结局大不一样. 人类受孕后,从一个单细胞逐渐发育成为一个 ...

  5. vsftpd启动报错:vsftpd:500 OOPS: bad bool value in config file for: anonymous_enable

    vsftpd启动报错:vsftpd:500 OOPS: bad bool value in config file for: anonymous_enable  今天在调试centos vsftp的时 ...

  6. 【BZOJ4031】【Luogu P4111】[HEOI2015]小Z的房间

    裸的矩阵树定理.求行列式的时候答案要在中间统计,因为交换两个行会使答案取反. #include <bits/stdc++.h> using namespace std; const int ...

  7. PHP商城的搜索功能

    大家好,今天分享一个商城的搜索功能,建立在上一篇文章的基础上实现的. 搜索功能简单的说就是通过sql语句在数据库中实现模糊查找 连接数据库,实现分页功能(可以参考上一篇文章) 定义一个变量接收传过来的 ...

  8. websocket练习

    html代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> < ...

  9. JS拖动滑块验证

    使用这种验证方法的目的:证明当前的用户不是机器人~防止恶意操作. 实现思路: 1.获取silde滑块(获取元素) 2.为元素注册事件———鼠标点击事件(onmousedown)鼠标点击之后获得当前鼠标 ...

  10. node 的fs.state 获取文件信息

    1. fs.stat()可以获取文件的信息,用法如下: const fs = require('fs'); fs.stat('./book.js',(err,stats)=>{ if(err) ...