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.

两种方法:1. 利用 hash_map 结构,数组有序时查找的思想。

class Solution {
public:
int longestConsecutive(vector<int> &num) {
if(num.size() == 0) return 0;
unordered_map<int, bool> _map;
for(size_t i = 0; i < num.size(); ++i)
_map.insert(pair<int ,bool>(num[i], false));
int ans = 0;
for(auto it = _map.begin(); it != _map.end(); ++it) {
if(it->second) continue; // visited
int len = 1;
int v1 = it->first-1;
while(_map.count(v1)) { _map[v1--] = true; len++; }
int v2 = it->first+1;
while(_map.count(v2)) { _map[v2++] = true; len++; }
if(len > ans) ans = len;
}
return ans;
}
};

2. 动态的构造线段(两端为线段始末位置),这样从一端点可获取另一端点位置。若当前点可增加有向线段长度,拓展线段。

class Solution {
public:
int longestConsecutive(vector<int> &num) {
int answer = 0;
unordered_map<int ,int>_map;
int low, high;
for(int i = 0; i < num.size(); ++i) {
if(_map.count(num[i])) continue; // must be used.
_map[num[i]] = num[i];
low = high = num[i];
if(_map.count(num[i]-1)) low = _map[num[i]-1];
if(_map.count(num[i]+1)) high = _map[num[i]+1];
answer = max(answer, high - low + 1);
if(low == high) continue;// not in a line
_map[low] = high;
_map[high] = low;
}
return answer;
}
};

24. Longest Consecutive Sequence的更多相关文章

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

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

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

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

  3. Binary Tree Longest Consecutive Sequence

    Given a binary tree, find the length of the longest consecutive sequence path (连续的路径,不是从小到大). The pa ...

  4. 128. Longest Consecutive Sequence(leetcode)

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

  5. [LintCode] Longest Consecutive Sequence 求最长连续序列

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

  6. LeetCode Binary Tree Longest Consecutive Sequence

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

  7. 【leetcode】Longest Consecutive Sequence

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

  8. 【LeetCode OJ】Longest Consecutive Sequence

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

  9. 298. Binary Tree Longest Consecutive Sequence

    题目: Given a binary tree, find the length of the longest consecutive sequence path. The path refers t ...

随机推荐

  1. 【59测试】【树】【dp】

    第一题 A : 这棵树由n个节点以及n - 1条有向边构成,每条边都从父亲节点指向儿子节点,保证除了根节点以外的每个节点都有一个唯一的父亲.树上的节点从1到n标号.该树的一棵子树的定义为某个节点以及从 ...

  2. uboot 链接地址与运行地址的区别

    对于ARM架构的CPU,上电后PC寄存器是指向0地址处的,从这个地址开始运行程序,那么运行了启动代码后会把程序搬移到内存中去运行,这样就是产生程序会在运行时有个两地址,而由源码编译为可执行文件时只会指 ...

  3. 深入理解gradle编译-Android基础篇

    深入理解gradle编译-Android基础篇 导读 Gradle基于Groovy的特定领域语言(DSL)编写的一种自动化建构工具,Groovy作为一种高级语言由Java代码实现,本文将对Gradle ...

  4. docker 源码分析 二(基于1.8.2版本),docker client与daemon交互

    (2) 那我们通过docker客户端发送一个命令,docker是怎样接收到并处理的呢,我们就举个例子来看一下,比如docker pull 命令: 我们回到 docker/docker.go 中,在上一 ...

  5. systemtap折腾笔记

    在这货上花费了不少时间,都是受了@agentzh 大神的蛊惑:) 他写的nginx-systemtap-toolkit监测的数据很有价值,对于系统优化实在是利器. 最早折腾systemtap,是在Ub ...

  6. Python Scarpy安装包

    由于网络的原因,Scraoy无法安装 Cannot fetch index base URL https://pypi.python.org/simple/ 1.  scrapy 安装所需要的包可以从 ...

  7. 自定义EditText实现可以一键删除输入的内容

    public class MyEditText extends EditText { private Drawable dRight; private Rect rRounds; public MyE ...

  8. php开发工具。。

    看了好多决定用phpstorm. hahaha PHP还是挺好玩的 但是貌似犯蠢一下,MAC自带有php环境: 我还下了一个XAMPP,不过无所谓啦. 都可以用

  9. C# 中Join( )的理解

    在MSDN中对Join( )的解释比较模糊:在继续执行标准的 COM 和 SendMessage 消息泵处理期间,阻塞调用线程(线程A),直到某个线程终(线程B)止为止. 首先来看一下有关的概念: 我 ...

  10. 黑马程序员——JAVA基础之编码表

    ------- android培训.java培训.期待与您交流! --------- 字符编码  字符流的出现为了方便操作字符.  更重要是的加入了编码转换.  通过子类转换流来完成. •  I ...