【LeetCode-128】Longest Consecutive Sequence
描述
输入一个乱序的连续数列,输出其中最长连续数列长度,要求算法复杂度为 O(n) 。
输入
54,55,300,12,56
输出
3
通常我们看有没有连续序列时 找某个数有没有的前后的数,比如看到55,我们会下意识找56,57...54,53...这道题做法也是这样。
我的做法是用map映射,键为列表中的每个数,值为1。然后遍历数组,比如数为55,找这个数的前后有没有map映射后值为1的,有的话sum++,为了防止重复遍历需要把遍历过的数的映射值改成-1。
class Solution
{
public:
int longestConsecutive(vector<int>& nums)
{
map<int, int>M;
int len = nums.size(), ans = ;
for(int i = ; i < len; i++)
M[nums[i]] = ;
for(int i = ; i < len; i++)
{
if(M[nums[i]] == -) continue;
int sum = , j = nums[i];
while()
{
M[j] = -;
if(M[++j] == )
{
sum++;
continue;
}
break;
}
j = nums[i];
while()
{
M[j] = -;
if(M[--j] == )
{
sum ++;
continue;
}
break;
}
ans = max(ans, sum);
}
return ans;
}
};
【LeetCode-128】Longest Consecutive Sequence的更多相关文章
- 【LeetCode OJ】Longest Consecutive Sequence
Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classi ...
- 【leetcode】Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- LeetCode Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- [LeetCode] Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之二
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
- 【leetcode】Longest Consecutive Sequence(hard)☆
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(128) Longest Consecutive Sequence
题目 Given an unsorted array of integers, find the length of the longest consecutive elements sequence ...
- 【LeetCode OJ】Longest Palindromic Substring
题目链接:https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...
随机推荐
- 130. Surrounded Regions(周围区域问题 广度优先)(代码未完成!!)
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- WLAN QOS
1. 理解WLAN QOS 1.1 WLAN QOS简介 802.11的WLAN网络为用户提供了公平竞争无线资源的无线接入服务,但不同的应用需求对于网络的要求是不同的,而原始802.11网 ...
- [转]运动检测(前景检测)之(二)混合高斯模型GMM
转自:http://blog.csdn.net/zouxy09/article/details/9622401 因为监控发展的需求,目前前景检测的研究还是很多的,也出现了很多新的方法和思路.个人了解的 ...
- 详解Java中的clone方法 -- 原型模式
转自: http://blog.csdn.net/zhangjg_blog/article/details/18369201 Java中对象的创建 clone顾名思义就是复制, 在Java语言中, ...
- java之继承中的构造方法
继承中的构造方法 1.子类的构造过程中必须调用其基类的构造方法. 2.子类可以在自己的构造方法中使用super(argument_list)调用基类的构造方法. 2.1.使用this(argumen ...
- v4l2的学习建议和流程解析
v4l2,一开始听到这个名词的时候,以为又是一个很难很难的模块,涉及到视频的处理,后来在网上各种找资料后,才发现其实v4l2已经分装好了驱动程序,只要我们根据需要调用相应的接口和函数,从而实现视频的获 ...
- 入手sm961
测速: 发现这个测速软件不同版本测试还不一样 下面是我的intel750的,用最新版本测试软件测的 淘宝买了一个散热片
- windows环境下mysql的解压安装以及备份和还原
系统环境为server2012 1.下载mysql解压版,解压安装包到指定目录 2.在以上目录中,复制一份my-default.ini文件,重命名为my.ini,进行如下修改(按照需要): [mysq ...
- This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery' 解决办法
背景:mysql5.1.36,mybatis 前言:为了解决一对多,分页显示,但是前端主要是显示的一的一方的数据和(多方的某个字段拼接在一起),此时的limit不能直接跟在查询的后面,需要用子查询把需 ...
- sqlite的Top筛选
select [CollectDateTime] as '时间',[Channel_34] as '通道34',[Channel_54] as '通道54' from [DataTable] wher ...