leetcode解题报告(5):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.
分析
对于这类题目,我们最自然的想法就是先排序,再对每一个数据进行处理。但是最常用的排序算法快速排序的平均运行时间也有O(nlgn),插入排序在最好的情况下才能达到O(n)的复杂度。
因此,这题不能用“先排序,再处理”的思路,可以考虑使用哈希表unordered_map<int,bool>。
该map记录每个元素是否被使用,对每个元素,以该元素为中心,分别向两边扩张,并记录扩张的长度,直到不连续时为止。
如:考察数组 [100, 4, 200, 1, 3, 2],从第一个元素(100)开始遍历每一个元素,分别查找向左(减一)和向右(加一)后的元素是否在该数组中,若在数组中,则将长度加一,同时将bool值置为true,以表示“该元素已和数组中的另一元素连续,不必再对该元素进行处理”。否则,退出循环。
这样,我们就能得到每一个元素的最大长度,只要在每一次遍历后更新最大长度就可以了。
代码如下:
class Solution{
public:
int longestConsecutive(const vector<int>&nums){
int longest = 0; //record the longest consecutive
unordered_map<int,bool>used;
for(auto num : nums){
int length = 1;
used[num] = true;
for(int next = num + 1; used.find(next) != used.end(); ++next){
used[next] = true;
++length;
}
for(int pre = num - 1; used.find(pre) != used.end(); --pre){
used[pre] = true;
++length;
}
longest = max(longest,length);
}
return longest;
}
};
leetcode解题报告(5):Longest Consecutive Sequence的更多相关文章
- [LeetCode] 298. Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] 549. Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之 II
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
- LeetCode 549. Binary Tree Longest Consecutive Sequence II
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence-ii/description/ 题目: G ...
- LeetCode 298. Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- [LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
- LeetCode(128) Longest Consecutive Sequence
题目 Given an unsorted array of integers, find the length of the longest consecutive elements sequence ...
- leetcode解题报告 32. Longest Valid Parentheses 用stack的解法
第一道被我AC的hard题!菜鸡难免激动一下,不要鄙视.. Given a string containing just the characters '(' and ')', find the le ...
- leetcode解题报告 32. Longest Valid Parentheses 动态规划DP解
dp[i]表示以s[i]结尾的完全匹配的最大字符串的长度. dp[] = ; ; 开始递推 s[i] = ')' 的情况 先想到了两种情况: 1.s[i-1] = '(' 相邻匹配 这种情况下,dp ...
- [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LintCode] 619 Binary Tree Longest Consecutive Sequence III 二叉树最长连续序列 III
Given a k-ary tree, find the length of the longest consecutive sequence path. The path could be star ...
随机推荐
- 2020年AI、CV、NLP顶会最全时间表
2020年AI.CV.NLP顶会最全时间表 2019-09-01 14:04:19 weixin_38753768 阅读数 40 2020 AI.CV.NLP主流会议时间表,包含会议举办的时间.地 ...
- python03-break、continue、for循环、数据bytes类型、字符串与字节的关系、变量指向与深浅拷贝、set集合、文件操作
目录: 1.break.continue 2.for循环 3.数据bytes类型 4.字符串与字节的关系 5.变量指向与深浅拷贝 6.set集合 7.文件操作 一.break.continue bre ...
- 入手线段树 hdu1754
今天学习了线段树的三个基本操作 建树 更新 查找 先理解下什么是线段树就这个题目而言 如果我们用普通的数组去存放 然后依次遍历访问的话 时间太多了线段树利用了二分的思想 把数据以段的形式进行储存 这样 ...
- [书籍翻译] 《JavaScript并发编程》 第二章 JavaScript运行模型
本文是我翻译<JavaScript Concurrency>书籍的第二章 JavaScript运行模型,该书主要以Promises.Generator.Web workers等技术来讲解J ...
- SQL alchemy
SQL alchemy介绍 SQL alchemy是orm思想的一个具体实现的产品 orm:对象关系映射思想 Object Relational Mapping 就是将数据库里的资源与面向对象中的类和 ...
- rsync & sersync 实时同步
1.根据之前一篇关于rsync的随笔部署好rsync服务后,可以开始inotify的部署 2.sersync的部署 ①.部署服务(安装和配置过程) #Master 部署Sersync服务 mkdir ...
- java与.net_20190726
特性与注解 方法首字母 Java没有ref和out关键字 Java的main方法与C#的Main方法不一样: Java的io类和C#不一样. java的jsp和aspx还是很类似的. Java的ser ...
- Juit
Junit这种老技术,现在又拿出来说,不为别的,某种程度上来说,更是为了要说明它在项目中的重要性. 凭本人的感觉和经验来说,在项目中完全按标准都写Junit用例覆盖大部分业务代码的,应该不会超过一半. ...
- SQL SERVER 2008 数据库隔离级别代码演示
SQL SERVER 2008 数据库隔离级别代码演示 个隔离级别(其实这是SQL 工业标) 种隔离级别,本身没有优劣之分,完全取决于应用的场景. 本质上,他们是在 隔离性(紊乱程度) 和 灵活性 ...
- Wannafly挑战赛24-A-石子游戏--【思维题】
链接:https://www.nowcoder.com/acm/contest/186/A 来源:牛客网 石子游戏 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他 ...