方法一:使用快排:

//排序法,时间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. Clob类型转换为String

    SQL CLOB 是内置类型,它将字符大对象存储为数据库表某一行中的一个列值,使用CHAR来存储数据,如XML文档. 如下是一个Clob转换为String的静态方法,可将其放在自己常用的工具类中,想直 ...

  2. JavaScript入门学习之一——初级语法

    JavaScript是前端编辑的一种编程语言(不同于html,html是一种标记语言),所以和其他的编程语言一样,我们将会从下面几点学习 基础语法 数据类型 函数 面向对象 JavaScript的组成 ...

  3. 权限和ACL访问控制 -01-权限

    权限位 rwxrwrwx:左三位:定义user(owner)的权限,属主权限中三位:定义group的权限,属组权限有三位:定义other的权限,其他的权限 进程对文件的访问权限应用模型:进程的属主与文 ...

  4. 【BZOJ3534】【Luogu P3317】 [SDOI2014]重建 变元矩阵树,高斯消元

    题解看这里,主要想说一下以前没见过的变元矩阵树还有前几个题见到的几个小细节. 邻接矩阵是可以带权值的.求所有生成树边权和的时候我们有一个基尔霍夫矩阵,是度数矩阵减去邻接矩阵.而所谓变元矩阵树实际上就是 ...

  5. JavaWEB开发05_Bootstrap

    上次课内容:什么JQ : write less do more 写更少的代码,做更多的事情 javascript函数库 基本选择器: ​ ID选择器: #ID名称 ​ 类选择器: .类名 ​ 元素选择 ...

  6. hiho #1474 拆字游戏(dfs,记录状态)

    #1474 : 拆字游戏 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Kui喜欢把别人的名字拆开来,比如“螺”就可以拆成“虫田糸”,小Kui的语文学的不是很好,于是 ...

  7. 【JZOJ5439】【NOIP2017提高A组集训10.31】Calculate

    题目 分析 对于\[\sum_{i=1}^{n}\lfloor\dfrac{T-B_i}{A_i}\rfloor\] 我们考虑拆开处理,得到 \[\sum_{i=1}^{n}(\lfloor\dfra ...

  8. 【leetcode】1012. Numbers With Repeated Digits

    题目如下: Given a positive integer N, return the number of positive integers less than or equal to N tha ...

  9. Python CGI编程Ⅲ

    GET和POST方法 浏览器客户端通过两种方法向服务器传递https://www.xuanhe.net/信息,这两种方法就是 GET 方法和 POST 方法. 使用GET方法传输数据 GET方法发送编 ...

  10. 未能从程序集“netstandard, Version=2.0.0.0......”中加载类型“...”。

      需要在机器上安装:4.7.1的framework 下载地址: https://www.microsoft.com/zh-CN/download/details.aspx?id=56116 说明:n ...