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. CentOS Mysql 5.1.73 主从配置

    ---------------------------------------------- 1 修改my.cnf ------------------------------------------ ...

  2. 【0 - 1】OC内存管理

    一.内存管理概述 垃圾回收机制(GC):由系统管理内存,程序员不需要管理. OC中的垃圾回收:在OC2.0版加入垃圾回收. OC与iOS:OC有垃圾回收机制,但是iOS屏蔽了这个功能.原因:iOS运行 ...

  3. Python学习路程day8

    Socket语法及相关 socket概念 A network socket is an endpoint of a connection across a computer network. Toda ...

  4. Ambari是什么?

    Ambari目标 解决Hadoop生态系统部署 部署:hadoop组件间有依赖,包括配置.版本.启动顺序.权限配置等. 部署过程跟踪.能够展示出部署过程中每个步骤的状态及相关信息. 多机部署问题,当集 ...

  5. BZOJ 3110 树套树 && 永久化标记

    感觉树套树是个非常高深的数据结构.从来没写过 #include <iostream> #include <cstdio> #include <algorithm> ...

  6. codeforces 711D Directed Roads(DFS)

    题目链接:http://codeforces.com/problemset/problem/711/D 思路:由于每个点出度都为1,所以没有复杂的环中带环.DFS遍历,若为环则有2^k-2种,若为链则 ...

  7. C#常用操作类库三(XML操作类)

    /// <summary> /// XmlHelper 的摘要说明. /// xml操作类 /// </summary> public class XmlHelper { pr ...

  8. Linux上vi(vim)编辑器使用教程

    vi(vim)是上Linux非常常用的编辑器,很多Linux发行版都默认安装了vi(vim).vi(vim)命令繁多但是如果使用灵活之后将会大大提高效率.vi是“visual interface”的缩 ...

  9. js常用字符串方法汇总

    concat()将两个或多个字符的文本组合起来,返回一个新的字符串. var a = "hello"; var b = ",world"; var c = a. ...

  10. 多线程下HttpContext.Current 的问题

    在项目中需要记录文本日志,为了加快响应速度所以用到了多线程. 但是以前的方法是不支持多线程的,程序运行错误. 追踪代码发现提示HttpContext为空. 1.HttpContext.Current表 ...