LeetCode 128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
For example,
Given [100, 4, 200, 1, 3, 2],
The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.
Your algorithm should run in O(n) complexity.
方法一:set实现
使用一个集合set存入所有的数字,然后遍历数组中的每个数字,如果其在集合中存在,那么将其移除,然后分别用两个变量pre和next算出其前一个数跟后一个数,然后在集合中循环查找,如果pre在集合中,那么将pre移除集合,然后pre再自减1,直至pre不在集合之中,对next采用同样的方法,那么next-pre-1就是当前数字的最长连续序列,更新res即可。
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
if(nums.size()==||nums.empty())
return ;
unordered_set<int> s(nums.begin(),nums.end());
int res=;
for(int val:nums)
{
if(!s.count(val))
continue;
s.erase(val);
int pre=val-,next=val+;
while(s.count(pre))
s.erase(pre--);
while(s.count(next))
s.erase(next++);
res=max(res,next-pre-);
}
return res;
}
};
方法二:map实现
刚开始哈希表为空,然后遍历所有数字,如果该数字不在哈希表中,那么我们分别看其左右两个数字是否在哈希表中,如果在,则返回其哈希表中映射值,若不在,则返回0,然后我们将left+right+1作为当前数字的映射,并更新res结果,然后更新d-left和d-right的映射值。
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
if(nums.size()==||nums.empty())
return ;
unordered_map<int,int> m;
int res=;
for(int val:nums)
{
if(!m.count(val))
{
int left=m.count(val-)?m[val-]:;
int right=m.count(val+)?m[val+]:;
int sum=left+right+;
res=max(res,sum);
m[val]=sum;
m[val-left]=sum;
m[val+right]=sum;
}
}
return res;
}
};
LeetCode 128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列的更多相关文章
- 128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列
给定一个未排序的整数数组,找出最长连续序列的长度.例如,给出 [100, 4, 200, 1, 3, 2],这个最长的连续序列是 [1, 2, 3, 4].返回所求长度: 4.要求你的算法复杂度为 O ...
- [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. F ...
- [leetcode]128. Longest Consecutive Sequence最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- Java for 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 ----- java
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Leetcode 128. Longest Consecutive Sequence (union find)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- Java算法-------无序数组中的最长连续序列---------leetcode128
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Leetcode#128 Longest Consecutive Sequence
原题地址 1. 把所有元素都塞到集合里2. 遍历所有元素,对于每个元素,如果集合里没有,就算了,如果有的话,就向左向右拓展,找到最长的连续范围,同时在每次找的时候都把找到的删掉.这样做保证了同样的连续 ...
随机推荐
- bzoj 4815 小Q的表格 —— 反演+分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4815 思路就和这里一样:https://blog.csdn.net/leolyun/arti ...
- Boost库之asio io_service以及run、run_one、poll、poll_one区别
一.io_service的作用 io_servie 实现了一个任务队列,这里的任务就是void(void)的函数.Io_servie最常用的两个接口是post和run,post向任务队列中投递任务,r ...
- JavaScript-Tool:my97datepicker
ylbtech-JavaScript-Tool:my97datepicker 1.返回顶部 1. 2.下载 https://files.cnblogs.com/files/storebook/java ...
- IIS及时回收
在打开的列表中更改以下设置:回收——固定时间间隔(分钟) 改为 0进程模型——闲置超时(分钟) 改为 0
- if if 和 if elif 的区别
再一次编程中意外使用了if if 也实现了 if elif的功能,所以搜索了下其中的区别: 1.if if 和 if elif 是有区别的,只是在某些情况下才会一样的效果: 2.随意使用会导致意外的错 ...
- 14.Nginx 文件名逻辑漏洞(CVE-2013-4547)
由于博主在渗透网站时发现现在Nginx搭建的网站是越来越多 所以对Nginx的漏洞来一个全面性的复习,本次从Nginx较早的漏洞开始分析. 2013年底,nginx再次爆出漏洞(CVE-2013-45 ...
- 在Ionic Framework 退出APP
使用以下代码: ionic.Platform.exitApp(); 在控制器中增加: $scope.Quit = function(){ ionic.Platform.exitApp(); }
- 【ssm整合打印sql语句】
#定义LOG输出级别log4j.rootLogger=INFO,Console,File #定义日志输出目的地为控制台log4j.appender.Console=org.apache.log4j.C ...
- BZOJ 1012【线段树】
题意: Q L 询问数列最后 L 个数中最大的数. A n 将 n + t ( t_init = 0 ), 然后插到最后去. 思路: 感觉动态地插入,很有问题. 数组地长度会时常变化,但是可以先预处理 ...
- bzoj2055: 80人环游世界(可行流)
传送门 表示完全看不懂最小费用可行流…… 据某大佬说 我们考虑拆点,然后进行如下连边 $s$向$a_i$连边,权值$0$,容量$[0,m]$ $a_i$向$a_i'$连边,权值$0$容量$[v_i,v ...