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.

思路:先把大神的方法亮出来

use a hash map to store boundary information of consecutive sequence for each element; there 4 cases when a new element i reached:

1) neither i+1 nor i-1 has been seen: m[i]=1;

2) both i+1 and i-1 have been seen: extend m[i+m[i+1]] and m[i-m[i-1]] to each other;

3) only i+1 has been seen: extend m[i+m[i+1]] and m[i] to each other;

4) only i-1 has been seen: extend m[i-m[i-1]] and m[i] to each other.

int longestConsecutive(vector<int> &num) {
unordered_map<int, int> m;
int r = ;
for (int i : num) {
if (m[i]) continue; //跳过重复
r = max(r, m[i] = m[i + m[i + ]] = m[i - m[i - ]] = m[i + ] + m[i - ] + ); //如果新的数字把左右连起来了,则把该连起来的序列的第一个数m[i - m[i - 1]]和最后一个数字m[i + m[i + 1]] 设为新的长度!
}
return r;
}

下面是我自己写的,可谓血与泪的历史。

首先O(n)表示一定不能排序, 那如何获得左右邻接的信息呢。 肯定要用hash了。结果花了2个小时,写了一个超复杂的代码。虽然AC,但是.......唉...................................................

int longestConsecutive(vector<int> &num) {
//去重复
unordered_set<int> s;
for(int i = ; i < num.size(); i++)
{
if(s.find(num[i]) == s.end())
s.insert(num[i]);
else
num.erase(num.begin() + (i--));
} int maxlen = ;
unordered_map<int, int> first; //记录每个连续序列的第一个数字在record中的哪个位置
unordered_map<int, int> last; //记录每个连续序列的最后一个数字在record中的哪个位置
vector<vector<int>> record; //记录每个连续序列的第一个数字和最后一个数字是什么
for(int i = ; i < num.size(); i++)
{
int pre, post;
unordered_map<int, int>::iterator f = first.find(num[i] + );
unordered_map<int, int>::iterator l = last.find(num[i] - );
if(f != first.end() && l != last.end())
{
pre = l->second;
post = f->second;
//修改hash表
last.erase(record[pre][]);
first.erase(record[post][]);
last[record[post][]] = pre;
//修改记录的区间
record[pre][] = record[post][];
maxlen = (maxlen > record[pre][] - record[pre][] + ) ? maxlen : record[pre][] - record[pre][] + ;
record[post].clear();
}
else if(f != first.end())
{
post = f->second;
//修改hash
first.erase(record[post][]);
first[num[i]] = post;
//修改区间
record[post][] = num[i];
maxlen = (maxlen > record[post][] - record[post][] + ) ? maxlen : record[post][] - record[post][] + ;
}
else if(l != last.end())
{
pre = l->second;
last.erase(record[pre][]);
last[num[i]] = pre; record[pre][] = num[i];
maxlen = (maxlen > record[pre][] - record[pre][] + ) ? maxlen : record[pre][] - record[pre][] + ;
}
else
{
record.push_back(vector<int>(, num[i]));
first[num[i]] = record.size() - ;
last[num[i]] = record.size() - ;
maxlen = (maxlen > ) ? maxlen : ;
}
}
return maxlen;
}

【leetcode】Longest Consecutive Sequence(hard)☆的更多相关文章

  1. 【leetcode】Longest Consecutive Sequence

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

  2. 【leetcode】Longest Common Prefix (easy)

    Write a function to find the longest common prefix string amongst an array of strings. 思路:找最长公共前缀 常规 ...

  3. leetcode 之Longest Consecutive Sequence(六)

    这题要仔细体会下哈希表的用法,要注意的是数组本身是无序的,因此需要向左右进行扩张. 另外这个思路可以进行聚类,把连续的标记为一类. int longestConsecutive(const vecto ...

  4. 【LeetCode-128】Longest Consecutive Sequence

    描述 输入一个乱序的连续数列,输出其中最长连续数列长度,要求算法复杂度为 O(n) . 输入 54,55,300,12,56 输出 3 通常我们看有没有连续序列时 找某个数有没有的前后的数,比如看到5 ...

  5. 【LeetCode】Longest Word in Dictionary through Deleting 解题报告

    [LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...

  6. 【SPOJ】Longest Common Substring(后缀自动机)

    [SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...

  7. 【LeetCode OJ】Longest Consecutive Sequence

    Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classi ...

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

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

  9. 【LeetCode】数组--合并区间(56)

    写在前面   老粉丝可能知道现阶段的LeetCode刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组( ...

随机推荐

  1. ASP.NET 5与MVC 6中的新特性

    差点忘了提一句,MVC 6中默认的渲染引擎Razor也将得到更新,以支持C# 6中的新语法.而Razor中的新特性还不只这一点. 在某些情况下,直接在Web页面中嵌入某些JSON数据的方式可能比向服务 ...

  2. 你可能不知道的Google Chrome命令行参数

    概述:              关于Google Chrome命令行参数(英文叫Google Chrome Command line switches),是Chrome为了实现实验性功能.方便调试. ...

  3. Spring、Spring MVC、MyBatis整合文件配置详解

    原文  http://www.cnblogs.com/wxisme/p/4924561.html 主题 MVC模式MyBatisSpring MVC 使用SSM框架做了几个小项目了,感觉还不错是时候总 ...

  4. TCP的流模式与UDP的报文模式对比

    1       案例背景 在学习TCP-IP协议详解卷一时,读到介绍TCP协议的部分,发现TCP的首部是没有报文总长度字段的,而在UDP中是有的,对这个问题的思考引出了两者之间的区别. 2    案例 ...

  5. Ruby学习之module

    我们可以认为module是一个专门存放一系列方法和常量的工具箱. module和class非常像, 只是module不能创建实例也不能有子类, 它们仅仅能存放东西. 例如: module Circle ...

  6. ElasticSearch 2 (6) - 插件安装Head、Kopf与Bigdesk

    ElasticSearch 2 (6) - 插件安装Head.Kopf与Bigdesk 摘要 安装Elasticsearch插件Head.Kopf与Bigdesk 版本 elasticsearch版本 ...

  7. CSS DIV HOVER

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  8. ubuntu安装php常见错误集锦

    一.configure 报错 1.错误类型: Configure: error: Please reinstall the libcurl distribution-easy.h should be ...

  9. ASP.NET后台输出js大全,页面顶部、form表单中前面与后面、和UpdatePanel(ScriptManager、AJAX)输出JS

    Response.Write 与   Page.ClientScript.RegisterStartupScript 与 Page.ClientScript.RegisterClientScriptB ...

  10. [POJ2892]Tunnel Warfare

    [POJ2892]Tunnel Warfare 试题描述 During the War of Resistance Against Japan, tunnel warfare was carried ...