LeetCode——Longest Consecutive Sequence
LeetCode——Longest Consecutive Sequence
Question
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.
Subscribe to see which companies asked this question.
Hide Tags
Solution
这道题对时间复杂度要求比较高,排个序也得O(nlgn),所以想要得到O(n),那必须得用空间换时间,用上哈希表。 用哈希表的时候不要用set,因为set插入一个元素的时间复杂度是O(n),应该选择用unorder_set。
然后遍历每个数字的时候,考虑去集合中查找有没有比这个数大1或者小1的,有的话就继续查找。
我们会发现,遍历4的时候,得到的集合是1, 2, 3, 4, 遍历1的时候也是得到1, 2, 3, 4, 这就重复了呀,所以再遍历4的时候,就应该把找到的元素从集合中去掉,以免重复计算,加大了时间复杂度,具体实现如下:
class Solution {
public:
int longestConsecutive(vector<int> &num) {
unordered_set<int> table(num.begin(), num.end());
int res = 0;
for (int i : num) {
if (table.find(i) == table.end()) continue;
table.erase(i);
int pre = i - 1;
int next = i + 1;
while (table.find(pre) != table.end()) table.erase(pre--);
while (table.find(next) != table.end()) table.erase(next++);
res = max(res, next - pre - 1);
}
return res;
}
};
LeetCode——Longest Consecutive Sequence的更多相关文章
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- LeetCode: Longest Consecutive Sequence 解题报告
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- [leetcode]Longest Consecutive Sequence @ Python
原题地址:https://oj.leetcode.com/problems/longest-consecutive-sequence/ 题意: Given an unsorted array of i ...
- [LeetCode] Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- LeetCode: Longest Consecutive Sequence [128]
[题目] Given an unsorted array of integers, find the length of the longest consecutive elements sequen ...
- Leetcode: Longest Consecutive Sequence && Summary: Iterator用法以及ConcurrentModificationException错误说明
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- LeetCode—Longest Consecutive Sequence
题目描述: Given an unsorted array of integers, find the length of the longest consecutive elements seque ...
- [Leetcode] Longest consecutive sequence 最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [Leetcode] Longest Consecutive Sequence 略详细 (Java)
题目参见这里 https://leetcode.com/problems/longest-consecutive-sequence/ 这个题目我感觉很难,看了半天别人写的答案,才明白个所以然.下面的代 ...
随机推荐
- iOS侧面加shadow
UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:_bgView.bounds]; _bgView.layer.masksToBo ...
- 170228、Linux操作系统安装ELK stack日志管理系统--(1)Logstash和Filebeat的安装与使用
安装测试环境:Ubuntu 16.04.2 LTS 前言 (1)ELK是Elasticsearch,Logstash,Kibana 开源软件的集合,对外是作为一个日志管理系统的开源方案.它可以从任何来 ...
- 170215、监听器(HttpSessionListener)在开发中的应用
这个监听器主要用于网站在线超时退出的功能. OnlineUserListener实现了HttpSessionListener定义的两个方法: sessionCreated()和sessionDestr ...
- phantomjs学习之截图
1.创建pageload.js文件: pageload.js var page = require('webpage').create(); var address = 'https://www.ba ...
- python之MongoDB学习
import pymongo as pm # 获取连接 client = pm.MongoClient('localhost', 27017) # 端口号是数值型 # 连接数据库 db = clien ...
- 13.php面向对象
1.构造函数 public __construct() {} 2.析构函数 public __destruct() {} 3.对象调用属性 //数以调用时候要用 -> 而不是Java的点. ...
- .Vue.js大全
Vue起步 1.下载核心库vue.js bower info vue npm init --yes cnpm install vue --save vue2.0和1.0相比,最大的变化就是引入了Vir ...
- Maven 整合SSH框架之pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- 洛谷P4475 巧克力王国
洛谷P4475 巧克力王国 题目描述 巧克力王国里的巧克力都是由牛奶和可可做成的. 但是并不是每一块巧克力都受王国人民的欢迎,因为大家都不喜欢过于甜的巧克力. 对于每一块巧克力,我们设 x 和 y 为 ...
- 笔记-django学习链接
form表单学习 https://www.cnblogs.com/weiman3389/p/6225075.html python字符串replace()方法 https://www.cnblogs. ...