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.
最好想到的思路是排序,如果排序,最快也要O(NlogN),不符合要求。
以空间换时间:
代码:
int longestConsecutive(vector<int>& nums)
{
map<int,int> map1;
int len = 0;
int maxlen = len;
for(int i = 0;i<nums.size();i++)
{
//在nums中出现,则不为0,否则为0
map1[nums[i]]++;
}
int min1 = nums[0];
int min_up = min1;
int min_down = min1-1;
int i = 0;
while(i<nums.size())
{
if(map1[min_up]!=0)
{
len++;
map1[min_up] = 0;
min_up++;
}
if(map1[min_down]!=0)
{
len++;
map1[min_down] = 0;
min_down--;
}
if(map1[min_down]==0 && map1[min_up]==0)
{
if(maxlen<len)
maxlen = len;
cout<<maxlen<<endl;
len = 0;
while(map1[nums[i++]]==0&&i<nums.size());
if(i<nums.size())
{
min1 = nums[i-1];
min_up = min1;
min_down = min1-1;
}
else break;
}
}
return maxlen;
}
时间复杂度:O(N),同时引入了map,空间复杂度O(N).
LeetCode—Longest Consecutive Sequence的更多相关文章
- LeetCode——Longest Consecutive Sequence
LeetCode--Longest Consecutive Sequence Question Given an unsorted array of integers, find the length ...
- [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 sequence. F ...
- [Leetcode] Longest Consecutive Sequence 略详细 (Java)
题目参见这里 https://leetcode.com/problems/longest-consecutive-sequence/ 这个题目我感觉很难,看了半天别人写的答案,才明白个所以然.下面的代 ...
随机推荐
- IIS添加域名
前提:域名可用 1.打开网站,点击右侧 绑定 2.添加域名 点击确定 3.结果: ok 配置完成.
- linphone 在am335x的编译过程
环境变量: export PREFIX=/usr export HOSTTPL=arm-linux-gnueabihf export INSTALLDIR=/home/elinux/linphone/ ...
- Java中float/double取值范围与精度
Java浮点数 浮点数结构 要说清楚Java浮点数的取值范围与其精度,必须先了解浮点数的表示方法,浮点数的结构组成,之所以会有这种所谓的结构,是因为机器只认识01,你想表示小数,你要机器认识小数点这个 ...
- myeclipse工程重名后怎么更改deploy location?
http://zhidao.baidu.com/link?url=I9E16OYfxovPHqBrRWhYCI9TYNG_X-Whg_X7QrJiOBXBGEwi-6WYsC-Zi4Jcg9zd3ye ...
- EasyUI 创建Tree
tree可以被从标记创建.easyui tree应该定义在ul元素中.无序列表ul元素提供了基本tree结构.每一个li元素被产生一个tree节点,子ul元素产生父tree节点.例子: < ...
- Easyui Datagrid相同连续列合Demo之三
效果图: html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...
- 【转】【iOS测试系列】常用测试小插件的使用
背景介绍 由于iOS系统的限制,在非越狱的自动化测试中无法实现一些常用的功能,比如不同应用之间来回切换.模拟全局的点击事件等等.但是在越狱的环境下,这些限制就不存在了,我们可以利用各种小插件来实现我们 ...
- php 转码
//$names = iconv("UTF-8", "gb2312", $name); //等同于javascript encodeURI("电影&q ...
- MyBatis-Spring-Boot 使用总结
接 MyBatis-Spring 使用总结 . mybatis开发团队为Spring Boot 提供了 MyBatis-Spring-Boot-Starter . 首先,MyBatis-Sprin ...
- epplus excel数据导出(数据量有点大的情况) Web和Client
Asp.net MVC后台代码 public ActionResult Export() { OfficeOpenXml.ExcelPackage ep = new OfficeOpenXml.Exc ...