128. Longest Consecutive Sequence *HARD* -- 寻找无序数组中最长连续序列的长度
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.
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
int n = nums.size(), maxi = , cnt, i, k;
if(n <= )
return ;
unordered_set<int> numsSet;
for(i = ; i < n; i++)
{
numsSet.insert(nums[i]);
}
for(i = ; i < n; i++)
{
cnt = ;
numsSet.erase(nums[i]);
for(k = nums[i]+; numsSet.find(k) != numsSet.end(); k++)
{
cnt++;
numsSet.erase(k);
}
for(k = nums[i]-; numsSet.find(k) != numsSet.end(); k--)
{
cnt++;
numsSet.erase(k);
}
if(cnt > maxi)
maxi = cnt;
}
return maxi;
}
};
先遍历数组,把所有元素放到一个set中。
然后再遍历数组,对每个元素,在set中找比它大的连续数字和比它小的连续数字,若找到则计数器加一,同时从set中删除这个数字。
128. Longest Consecutive Sequence *HARD* -- 寻找无序数组中最长连续序列的长度的更多相关文章
- 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 ...
- 如何寻找无序数组中的第K大元素?
如何寻找无序数组中的第K大元素? 有这样一个算法题:有一个无序数组,要求找出数组中的第K大元素.比如给定的无序数组如下所示: 如果k=6,也就是要寻找第6大的元素,很显然,数组中第一大元素是24,第二 ...
- LeetCode 128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.Fo ...
- [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 ...
- 128. Longest Consecutive Sequence(leetcode)
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
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
随机推荐
- Berkeley DB的常见API简单分析
1.用来存储类信息的数据库不要求能够存储重复的关键字 例: dbConfig.setSortedDuplicates(false);2.DatabaseEntrt能够支持任何能够转化为by ...
- iOS 框架收集
检测硬件设备信息 https://github.com/Shmoopi/iOS-System-Services
- STL--queue
queue-概述: 队列是一种特殊的线性表,它只允许在表的前端(Front)进行删除操作,而在表的后端(Rear)进行插入操作. l进行插入操作的端称为队尾,进行删除操作的端称为队头.队列中没有元素时 ...
- 表单美化-原生javascript和jQuery单选按钮(兼容IE6)
最近很多人问怎么美化表单的元素,大家都知道表单元素在各个浏览器中的表现不一,反正也是特别的丑,那么问题就来了,我们能自己设计表单元素的外观么?答案是可以的,现在我们就来试试吧.我们用两种方式来实现这一 ...
- xml配置文件详解
1:bean的基本属性配置: <!-- id是bean的标识符,必须唯一,如果没有配置id,name默认为标识符 如果配置了id,有配置了name,那么name为别名 name可以设置多个别名, ...
- Python学习笔记8—语句
条件语句 有的程序里写的是 /usr/bin Python,表示 Python 解释器在/usr/bin 里面.但是,如果写成 /usr/bin/env,则表示要通过系统搜索路径寻找 Python 解 ...
- Three.js 3D特效学习
一.Three.js基本介绍 Three.js是JavaScript编写的WebGL第三方库.提供了非常多的3D显示功能.Three.js 是一款运行在浏览器中的 3D 引擎,你可以用它创建各种三维场 ...
- EMV技术学习和研究(转)
刚开始学习EMV&PBOC,磕磕碰碰,感谢xuture的<EMV技术学习和研究>给了很大帮助,让我少走了很多弯路,也感谢广俊.surge.艾零.小SO.Spinach.龙行天下的帮 ...
- Hadoop的HA集群启动和停止流程
假设我们有3台虚拟机,主机名分别是hadoop01.hadoop02和hadoop03. 这3台虚拟机的Hadoop的HA集群部署计划如下: 3台虚拟机的Hadoop的HA集群部署计划 hadoop0 ...
- 静态库冲突的解决办法:duplicate symbol
昨天在做微信sdk和xmpp的集成,发现各自单独集成没问题,一起集成却总报错,百度了好一会儿才知道,这应该是库冲突造成的问题 然后参考了很多文章,跟着敲敲一遍,却发现问题多多,最后主要综合结合了这两个 ...