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 ...
随机推荐
- [二叉树算法]让树所有叶子节点连成一个单链表,让rchild作为 next指针
//让树所有叶子节点连成一个单链表,让rchild作为 next指针 LNode *head=null,*pre=null;//全局变量 LNode *InOrder(BTNode *T){ if(T ...
- VMware 克隆的相关设置
点击管理--克隆,进行克隆操作完成后,进行下面设置: 1.删除原先PCI设置 vi /etc/udev/rules.d/70-persistent-net.rules 2.修改MAC地址及IP v ...
- CSS定位以及z-index属性(层叠性)的详解(转)
https://blog.csdn.net/weixin_41342585/article/details/79484879
- 图像识别领域的一些code
图像识别领域的一些code 转自:http://blog.163.com/pz124578@126/blog/static/23522694201343110495537/ ps:里面的一些方法都是目 ...
- POJ2729 Robocode(离散化与模拟-----提醒曾经爱玩游戏的自己没做出这个
题目链接 :http://poj.org/problem?id=2729 题目很长,有不少也是废话.类似小时候玩的坦克大战.每个坦克速度为10,炮弹速度为20.子弹出界就消失,坦克出不了界限.相向的子 ...
- 【多进程】php实现 master-worker 守护多进程模式
<?php class Worker{ public static $count = 2; public static function runAll(){ static::runMaster( ...
- 关于将多个json对象添加到数组中的测试
如果用数组push添加不到数组中的,这个我也不知道是为什么?然后我选择了另一种发放就是从数组出发,逆向添加 最后的数组是这样的: data1=['公司1','公司2','公司3','公司4']; ar ...
- pytorch之nn.Conv1d详解
转自:https://blog.csdn.net/sunny_xsc1994/article/details/82969867,感谢分享 pytorch之nn.Conv1d详解
- swoole聊天室
服务端: <?phpclass Chat{ const HOST = '0.0.0.0';//ip地址 0.0.0.0代表接受所有ip的访问 const PART = 8080;//端口号 pr ...
- oracle通过一个字段分组,取另一个字段的最大值
select * from bdcdj.lqentry1 a where 顺序号 in (select max(顺序号) from bdcdj.lqentry1 b WHERE b.archival ...