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/ 这个题目我感觉很难,看了半天别人写的答案,才明白个所以然.下面的代 ...
随机推荐
- 使用burpsuite来扫描漏洞
一张图说明全文.
- Java单元测试学习
单元测试的好处 1. 让你写出更好的代码:职业高内聚.低耦合而且接口设计合理的代码才易于测试: 2. 让你在修改代码时更有信心. JUnit4 注解 @Test (expected = Excepti ...
- Android基础总结(六)Activity
创建第二个Activity(掌握) 需要在清单文件中为其配置一个activity标签 标签中如果带有这个子节点,则会在系统中多创建一个快捷图标 <intent-filter> <ac ...
- 让rm命令提示确认后再删除
首先在~/.bashrc文件中添加一行: # User specific aliases and functionsalias rm='rm -i' 注意,此处 rm 和 = 之间不能有空格,否则会有 ...
- mysql_ado的demo
winform程序 http://pan.baidu.com/s/1nvxm5br
- ThinkPHP整合cropper剪裁图片上传功能
1.先下载核心文件:https://github.com/fengyuanchen/cropper 2. 3.对于index.html文件 4.对于main.js文件 5.对于crop.php文件 & ...
- 【C语言】求旋转数组的最小数字,输入一个递增排序的数组的一个旋转,输出其最小元素
//求旋转数组的最小数字,输入一个递增排序的数组的一个旋转,输出其最小元素 #include <stdio.h> #include <string.h> int find_mi ...
- 《linux系统及其编程》实验课记录(四)
实验4:组织目录和文件 实验目标: 熟悉几个基本的操作系统文件和目录的命令的功能.语法和用法, 整理出一个更有条理的主目录,每个文件都位于恰当的子目录. 实验背景: 你的主目录中已经积压了一些文件,你 ...
- 《Programming with Objective-C》第五章 Customizing Existing Classes
1.分类里面只新增函数,不要新增变量:虽然新增是语法合法的,但是编译器并不会为你的property合成相应的成员变量.setter和getter Categories can be used to d ...
- 【C++缺省函数】 空类默认产生的6个类成员函数
1.缺省构造函数. 2.缺省拷贝构造函数. 3. 缺省析构函数. 4.缺省赋值运算符. 5.缺省取址运算符. 6. 缺省取址运算符 const. <span style="font-s ...