leetcode 128最长连续序列

方法一:使用快排:
//排序法,时间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;
}
};
* * * * * /类解决方案{公众: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最长连续序列的更多相关文章
- Java实现 LeetCode 128 最长连续序列
128. 最长连续序列 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连 ...
- 图解leetcode —— 128. 最长连续序列
前言: 每道题附带动态示意图,提供java.python两种语言答案,力求提供leetcode最优解. 描述: 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). ...
- LeetCode 128. 最长连续序列(Longest Consecutive Sequence)
题目描述 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连续序列是 [1 ...
- 【LeetCode】128. 最长连续序列
题目 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为O(n). 示例: 输入:[100, 4, 200, 1, 3, 2] 输出:4 解释:最长连续序列是[1, 2, 3, ...
- leetcode.哈希表.128最长连续序列-Java
1. 具体题目 给定一个未排序的整数数组,找出最长连续序列的长度.要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连续序列是 ...
- leetcode 128. 最长连续子序列
题目描述: 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入:[100, 4, 200, 1, 3, 2] 输出:4 即最长的连续序列为 [1,2, ...
- 【LeetCode】最长连续序列
[问题]给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [, , , , , ] 输出: 解释: 最长连续序列是 [, , , ].它的长度为 ...
- [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [leetcode]128. Longest Consecutive Sequence最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
随机推荐
- 谷歌浏览器(Chrome)离线包的下载方法!
谷歌浏览器(Chrome)其实可以下载离线包,用离线包安装的好处,就是一次获得全部安装文件,不需要漫长的在线下载过程了! 下载地址:https://www.google.com/chrome/eula ...
- 惟一ID生成方法
几乎所有的业务系统,都存在生成惟一ID的需求,例如: 用户ID:user_id 订单ID: order_id 消息ID: msg_id 常见的ID生成有三大类方法: 一.中间件实现 1.利用Mysql ...
- (转)Ubuntu换源方法
I. 查看系统版本及内核 首先查看自己的ubuntu系统的codename,这一步很重要,直接导致你更新的源是否对你的系统起效果,查看方法: lsb_release -a 如,我的系统显示: No L ...
- Cypress自动化测试系列之二
本文技术难度★★★,如果前编内容顺利执行,请继续. 如果Selenium尚无法灵活运用的读者,本文可能难度较大. “理论联系实惠,密切联系领导,表扬和自我表扬”——我就是老司机,曾经写文章教各位怎么打 ...
- 简单xml示例
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.X ...
- 【洛谷P1450】硬币购物
题目大意:给定 4 种面值的硬币和相应的个数,求购买 S 元商品的方案数是多少. 题解: 考虑没有硬币个数的限制的话,购买 S 元商品的方案数是多少,这个问题可以采用完全背包进行预处理. 再考虑容斥, ...
- MyEclipse使用教程:导航代码(一)
[MyEclipse CI 2019.4.0安装包下载] 无论是在文件之间导航还是在文件中导航,都可以使用大量导航工具来加快工作流程.目前这些导航工具可在MyEclipse,CodeMix中使用. 快 ...
- sed基础语法
sed 太强大了 参考博客如下:https://www.cnblogs.com/ctaixw/p/5860221.html sed: Stream Editor文本流编辑,sed是一个“非交互式的”面 ...
- 32.把数组排成最小的数(python)
题目描述 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. # -*- ...
- Java中截取字符串中小数点前面的字符
通过下标获取 String number = "2563.2154"; int index = number.indexOf("."); String intN ...