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 ...
随机推荐
- 数据库 (二):MySQL密码策略与用户管理
为了加强安全性,MySQL5.7为root用户随机生成了一个密码可通过# grep "password" /var/log/mysqld.log 命令获取MySQL的临时密码用该密 ...
- python学习之路入门篇
本文是up学习python过程中遇到的一些问题及总结归纳,本小节是入门篇. python基本语法 循环.分支不多赘述. 模块:一个.py文件就是一个模块. 文件和异常 模式 含义解释 “r” 读模式 ...
- 4.pca与梯度上升法
(一)什么是pca pca,也就是主成分分析法(principal component analysis),主要是用来对数据集进行降维处理.举个最简单的例子,我要根据姓名.年龄.头发的长度.身高.体重 ...
- linux 查看cpu核心数
1.查看CPU个数 cat /proc/cpuinfo |grep "physical id"|sort|uniq|wc -l 2.查看每个物理CPU含有的核心个数 cat /pr ...
- 自定义前端框架(VUE+magicbox响应式风格)
1.用脚手架初始化一个vue项目swain $ vue create swain 2.安装几个常用插件
- VMware Horizon Client剪贴板异常问题解决
接到用户反馈现象是:登录ERP系统操作是,无法复制粘贴本地电脑上的数据. 处理过程: 1.在域控服务器上建立独立的Horizon Computer OU,把所有RDS加入在改OU中 2.针对Horiz ...
- tomcat9.0 问题汇总
安装时提示 Failed installing tomcat9 service 是因为之前安装tomcat,然后直接删除文件夹,虽然把文件夹删除了,但是重新安装时,服务存在相同的服务名,解决办法:使用 ...
- poj1952 BUY LOW, BUY LOWER[线性DP(统计不重复LIS方案)]
如题.$N \leqslant 5000$. 感觉自己思路永远都是弯弯绕绕的..即使会做也会被做繁掉..果然还是我太菜了. 递减不爽,先倒序输入算了.第一问做个LIS没什么说的.第二问统计个数,考虑什 ...
- 【hiho1065】全图传送
题目大意:给定一棵 N 个节点的树,点有点权,边有边权,给定 M 个询问,每次询问距离 U 节点不超过 R 的点集中,点权最大的点的编号是多少,若有相同点权,取编号较小的点. 题解: 发现是多组询问, ...
- 搭建web服务器---Apache服务器
一.安装Apache 尽管xampp appserv等提供了方便的集成环境,但实际上部署环境还是下载单独安装的Apache,以避免那些安全问题 二.加载php解析模块,并指定模块处理文件的类型 编辑h ...