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.

想法一:

用01数组代表该数字是否存在序列中,化归为:一个01序列中,最长连续1的问题。

小数据上可以,但是大数据测试上遇到了问题,当数据跨度很大,比如INT_MIN~INT_MAX时,空间复杂度太高。

想法二:

用map代替数组实现上面的想法,可以解决空间复杂度问题。

但是在计算最长连续1的过程中发现新的问题,遍历INT_MIN~INT_MAX的时间复杂度太高。

解法:

对num数组中的元素进行左右最大扩展计数,代替全范围的遍历计数。

class Solution {
public:
int longestConsecutive(vector<int> &num) {
int ret = ;
unordered_map<int, bool> m;
for(int i = ; i < num.size(); i ++)
m[num[i]] = false; for(int i = ; i < num.size(); i ++)
{
if(m[num[i]] == false)
{
int cur = ;
int left = num[i]-;
while(m.find(left) != m.end() && m[left] == false && left >= INT_MIN)
{
m[left] = true;
cur ++;
left --;
}
int right = num[i]+;
while(m.find(right) != m.end() && m[right] == false && right <= INT_MAX)
{
m[right] = true;
cur ++;
right ++;
}
ret = max(ret, cur);
}
}
return ret;
}
};

【LeetCode】128. Longest Consecutive Sequence的更多相关文章

  1. 【leetcode】1027. Longest Arithmetic Sequence

    题目如下: Given an array A of integers, return the length of the longest arithmetic subsequence in A. Re ...

  2. 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)

    [LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...

  3. [LeetCode] 128. Longest Consecutive Sequence 解题思路

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  4. [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  5. 128. Longest Consecutive Sequence(leetcode)

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  6. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  7. LeetCode Binary Tree Longest Consecutive Sequence

    原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...

  8. [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 ...

  9. Java for LeetCode 128 Longest Consecutive Sequence

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

随机推荐

  1. 如何实现JS函数的重载

    javascript不能支持函数的重载,如下: function f(length) { alert("高为:"+length); } function f(length,widt ...

  2. scala 学习笔记十 一 伴生对象

    1.介绍 a.所谓伴生对象就是和某个class同名的object, 并且object 必须和class在同一个scala源文件中. b.在scala中,没有像java中的静态类,静态方法和静态成员等, ...

  3. php过滤文字中的表情字符和mysql服务端对emoji的支持

    1.过滤emoji表情的原因 在我们的项目开发中,emoji表情是个麻烦的东西,即使我们可以能存储,也不一定能完美显示,因为它的更新速度很快:在iOS以外的平台上,例如PC或者android.如果你需 ...

  4. [Backbone]3. More detail on View

    Change the AppointmentView to have a top-level li tag (instead of the default div tag). var Appointm ...

  5. Discuz常见小问题-网站如何备份和恢复

    进入后台之后,点击UCenter,然后在左侧的数据备份中点击提交,大概一两分钟会显示备份完成 备份好的数据在uc_server的data/backup文件夹中(文件名为时间+随机字符,里面就一个数据库 ...

  6. Java8新特性 - Lambda表达式 - 基本知识

    A lambda expression is an unnamed block of code (or an unnamed function) with a list of formal param ...

  7. PHP高级教程-包含

    PHP 包含文件 PHP include 和 require 语句 在 PHP 中,您可以在服务器执行 PHP 文件之前在该文件中插入一个文件的内容. include 和 require 语句用于在执 ...

  8. 文件的上传Commons FileUpload(web基础学习笔记十六)

    一.表单设置 <form action="<%=request.getContextPath()%>/jsp/admin/doAdd.jsp" enctype=& ...

  9. spring常用注解以IOC理解

    使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册<context:component-scan base-package ...

  10. Openerp 7.0 附件存储位置

    我们知道对OpenERP中的每个内部对象(比如:业务伙伴,采购订单,销售订单,发货单,等等)我们都可以添加任意的附件,如图片,文档,视频等.那么这些附件在OpenERP内部是如何管理的呢? 默认情况下 ...