方法一:使用快排:

//排序法,时间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. TensorFlow入门——hello

    上一节说了TensorFlow的安装,这一节说一下测试的问题 新建一个Python文件,输入 import tensorflow as tf hello = tf .constant (’Hello, ...

  2. Zookeeper启动失败,报错 can not open chanel to 2

    zookeeper 3.4.8 安装在 7 台不同的虚拟机上,配置文件如下: tickTime= initLimit= syncLimit= dataDir=/var/zookeeper client ...

  3. MySQL中导出用户权限设置的脚本

    在对MySQL数据库进行迁移的时候,有时候也需要迁移源数据库内的用户与权限.对于这个迁移我们可以从mysql.user表来获取用户的相关权限来生成相应的SQL语句,然后在目标服务器上来执行生成的SQL ...

  4. 如何使用sqlalchemy根据数据库里面的表反推出模型,然后进行查询

    关于sqlalchemy映射数据库里面的表,一般情况下我们是需要定义一个模型来映射数据库里面的表的.但是很多时候数据库里面的表都是定义好的,而且字段很多,那么有没有不定义模型,还能使用orm语法查找数 ...

  5. zk和eureka的区别(CAP原则)

    作为服务注册中心,Eureka比Zookeeper好在哪里 著名的CAP理论指出,一个分布式系统不可能同时满足C(一致性).A(可用性)和P(分区容错性).由于分区容错性在是分布式系统中必须要保证的, ...

  6. cmd中subst的使用

    SUBST [drive1: [drive2:]path] drive1:        指定要指派路径的虚拟驱动器.[drive2:]path  指定物理驱动器和要指派给虚拟驱动器的路径. 路径替换 ...

  7. 树形DP Choosing Capital for Treeland

    给你一棵有向树,需要选定一个点为capital,满足翻转边数最小 思路:先求出1为capital 的答案,然后向下更新孩子节点 dp[i]=dp[i-1]+judge(i); #include< ...

  8. npoi c#

    没有安装excel docx的情况下 操作excel docx

  9. css 点击打开遮罩

    <div> <nav class="bar bar-tab"> <a class="tab-item external" href ...

  10. 【CF1187E】Tree Painting

    题目大意:给定一棵 N 个点的树,初始全是白点.要求你做 N 步操作,每一次选定一个与一个黑点相隔一条边的白点,将它染成黑点,然后获得该白点被染色前所在的白色联通块大小的权值.第一次操作可以任意选点, ...