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 ...
随机推荐
- Centos6 yum安装nginx
1.Centos6系统库中默认是没有nginx的rpn包的,所以我们需要先更新下rpm依赖库 (1):使用yum安装nginx,安装nginx库 rpm -Uvh http://nginx.org/p ...
- Java数据结构Vetor
Java数据结构Vector /** * <html> * <body> * <P> Copyright JasonInternational</p> ...
- MySQL修改默认编码 utf8
修改liunux下MySql默认编码 1安装mysql后,启动服务并登陆,使用status m命令发现mysql的编码并不是 utf8! mysql> status; 2关闭mysql 服务: ...
- Django rest-framework框架-组件之渲染器
渲染器: from rest_framework.renderers import BrowsableAPIRenderer,AdminRenderer,HTMLFormRenderer,JSONRe ...
- Django rest-framework框架-组件之视图
视图: a. django class Test(View): ... b. rest_framework class Test(APIView): ... c. GenericAPIView 一般不 ...
- windows开机自动执行bat脚本启动cmd命令窗口并执行命令,最后自动关闭cmd命令窗
先说bat脚本吧,随便在桌面建一个文本文档,在里面写脚本,内容是,先启动cmd命令窗,并执行一个命令,最后自动关闭cmd窗,如下: ---------------------------------- ...
- BPM软件_财务报销流程管理解决方案_K2工作流引擎
财务报销,对任何企业都是日常运营中重要的一个环节.但报销流程周期长,反复签字手续繁杂,报销过程不透明 ,单据归档保存.检索困难等问题也让员工头疼.为了解决这些困扰,财务报销流程电子化一时成为热门之选. ...
- flex布局实战
1.实现盒子的水平垂直居中 .parent{ width:200px; height:200px; display:flex; align-items: center; justify-content ...
- page页面403
nginx 没有监听内网地址 必须要用域名访问
- redis运维相关
一.redis都有哪些数据类型?分别在哪些场景下使用比较合适?二.redis双写不一致三.雪崩和穿透四.redis的过期策略,LRU五.redis是如何实现高性能高并发六.如何保证Redis的高并发和 ...