LeetCode: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.
分析:
算法1:首先想到的是排序,排序后遍历一遍就可以找出最长连续序列的长度,只是要稍微注意下判断连续序列的过程中有可能两个元素相同,比如1 2 2 3,排序复杂度n*log(n),虽然题目要求O(n)复杂度,但是这个解法也可以通过OJ,代码如下:
1 class Solution { 2 public: 3 int longestConsecutive(vector<int> &num) { 4 // IMPORTANT: Please reset any member data you declared, as 5 // the same Solution instance will be reused for each test case. 6 int res = 1, len = num.size(); 7 if(len == 0)return 0; 8 sort(num.begin(), num.end()); 9 int curr = 1; 10 for(int i = 1; i < len; i++) 11 { 12 if(num[i] - num[i-1] == 1) 13 { 14 curr++; 15 if(curr > res)res = curr; 16 } 17 else if(num[i] - num[i-1] == 0); 18 else 19 curr = 1; 20 } 21 return res; 22 } 23 };
View Code
算法2:想要O(n)的算法,我们只有以时间换空间,先把数组中所有元素映射到哈希表。然后以题目给出的数组为例:对于100,先向下查找99没找到,然后向上查找101也没找到,那么连续长度是1,从哈希表中删除100;然后是4,向下查找找到3,2,1,向上没有找到5,那么连续长度是4,从哈希表中删除4,3,2,1。这样对哈希表中已存在的某个元素向上和向下查找,直到哈希表为空。算法相当于遍历了一遍数组,然后再遍历了一遍哈希表,复杂的为O(n)。代码如下: 本文地址
1 class Solution { 2 public: 3 int longestConsecutive(vector<int> &num) { 4 // IMPORTANT: Please reset any member data you declared, as 5 // the same Solution instance will be reused for each test case. 6 int res = 1, len = num.size(); 7 if(len == 0)return 0; 8 unordered_set<int> hashtable; 9 for(int i = 0; i < len; i++) 10 hashtable.insert(num[i]); 11 while(hashtable.empty() == false) 12 { 13 int currlen = 1; 14 int curr = *(hashtable.begin()); 15 hashtable.erase(curr); 16 int tmp = curr-1; 17 while(hashtable.empty()==false && 18 hashtable.find(tmp) != hashtable.end()) 19 { 20 hashtable.erase(tmp); 21 currlen++; 22 tmp--; 23 } 24 tmp = curr+1; 25 while(hashtable.empty()==false && 26 hashtable.find(tmp) != hashtable.end()) 27 { 28 hashtable.erase(tmp); 29 currlen++; 30 tmp++; 31 } 32 if(res < currlen)res = currlen; 33 } 34 return res; 35 } 36 };
【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3422249.html
转载于:https://www.cnblogs.com/TenosDoIt/p/3422249.html
LeetCode:Longest Consecutive Sequence相关推荐
- LeetCode: Longest Consecutive Sequence [128]
[题目] Given an unsorted array of integers, find the length of the longest consecutive elements sequen ...
- [LeetCode] Longest Consecutive Sequence 求解
为什么80%的码农都做不了架构师?>>> 题目 Given an unsorted array of integers, find the length of the long ...
- leetcode Longest Consecutive Sequence
#include <iostream> #include <vector> #include <unordered_map>// 时间复杂度是O(n), 空间复杂度 ...
- LeetCode: Longest Consecutive Sequence
想到map了,可惜没想到用erase来节省空间,看了网上答案 1 class Solution { 2 public: 3 int longestConsecutive(vector<int&g ...
- [leetcode]Longest Consecutive Sequence
这道题目一看就去翻答案了,因为以前见过,知道并查集可以用来做分组,但这样的题目真的要并查集么?想想也是HashSet之类就能搞定么.果然是HashMap而不是HashSet.下面这个解法我比较容易理解 ...
- 298. Binary Tree Longest Consecutive Sequence
题目: Given a binary tree, find the length of the longest consecutive sequence path. The path refers t ...
- leetcode 128. Longest Consecutive Sequence | 128. 最长连续序列(Java)
题目 https://leetcode.com/problems/longest-consecutive-sequence/ 题解 方法1:HashMap 解法,O(n^2) 如下图,假设 n=4 被 ...
- LeetCode 128. 最长连续序列(Longest Consecutive Sequence)
题目描述 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连续序列是 [1 ...
- 128. Longest Consecutive Sequence
Title 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连续序列是 [ ...
最新文章
- 技术04期:关于神经网络的概念及技术领域
- SQL删除一个数据库内所有表的数据保留表结构
- 【SQLAlchemy】简单整理filter和filter_by的区别
- STL泛型编程之迭代器
- php 发邮件 500错误,使用PHP发送邮件出现这个错误:The following From address failed:xxxx@163.com...
- JavaWeb编程中如果jar包存在但显示无法输出,就在IDEA的项目发布中添加lib依赖!
- WebUI Case(1): www.swt-designer.com 首页
- Vuforia的ARcamera通过识别人工标识码出现虚拟物体后在其相机视野中不显示人工标识码
- 多终端房地产项目管理系统源码,源码分享
- php led显示屏控制软件下载,中航led控制软件
- 串联电阻分压计算器---实际常用电阻表
- Activity的启动流程分析与总结
- 私人用的红帽linux,红帽宣布为个人开发者提供16个RHEL免费许可 支持在生产环境中使用...
- 模仿微信九宫格、查看大图
- Windows7下Foxmail不能使用问题解决一例
- 【UI/UE设计师】banner设计原则-CSDN公开课-专题视频课程
- vnc桌面手机版,推荐5款好用的vnc桌面手机版软件
- 社会工程学与APT攻击之——社工邮件钓鱼分析
- 狄利克雷卷积学习记录
- 2021-10-06 求1到100以内的质数